# Formula basics

Formulas are a special language used on Conjointly to perform tasks such as:

## Algebraic functions

Algebraic functions perform basic mathematic operations:

OperationSyntaxExample
Addition`x+y``20+15` = 5
Subtraction`x-y``10-3` = 7
Multiplication`x*y``7*3` = 21
Division`x/y``10/2` = 5
Exponential`x^y``4^2` = 16

## Comparison operators

Comparison operators are used to compare two variables. Comparisons operators always return `TRUE` or `FALSE`.

OperationSyntaxExample
Equals`x==y``5==10` = FALSE
Not equals`x!=y``10!=3` = TRUE
Greater than`x>y``5>19` = FALSE
Equal to or greater than`x>=y``4>=4` = TRUE
Less than`x<y``6<10` = TRUE
Equal to or less than`x<=y``5<=1` = FALSE

## Boolean operators

Boolean operators are used to combine or exclude comparison operations. Boolean operators always return `TRUE` or `FALSE`.

OperationSyntaxExample
NOT`!x``!(10==5)` = TRUE
AND`x&y``TRUE&(10==5)` = FALSE
OR`x|y``(10<4)|(10>2)` = FALSE

## Built-in functions

### if or ifs functions

These functions allow for different return values depending on whether a condition is true or not

FunctionDescription
`if('condition', 'then', 'else')`Checks condition and returns a value if it is true or another value if it is false
`ifs('condition_1', 'then_1', 'condition_2', 'then_2',..., 'else')`Returns a value that corresponds to the first true condition or the else value if other conditions are false

### Respondent information functions

These functions are used to pull respondent information from for use in formulas

FunctionDescriptionExample
`GETvariable(variable_name)`Returns the GET variable specified by `variable_name`. `variable_name` should be the name of the variable quoted in single quotation marks. If none is supplied, returns NULL.`GETvariable('platform')`
`externalVariable(variable_name)`Returns value of the externally uploaded variable specified by `variable_name`. If none is supplied, returns NULL.`externalVariable('platform')`
`segregation(scenario_name)`Returns the segment the respondent was assigned via the segregation feature within the scenario specified by `scenario_name``segregation('baseline')`
`respondentInfo('variable_name')`Returns information automatically collected about a respondent.`respondentInfo('baseline')`

The list of valid variable names when returning respondent information using `respondentInfo` are:

Variable nameDescription
`ip`Returns the IP of the respondent.
`locationCountry`Returns the country of the respondent.
`locationRegion`Returns the region recorded for the respondent.
`device`Returns the device the respondent completed the survey on.
`locale`Returns locale recorded for the respondent.
`id`Returns the id used for the respondent. Respondent id’s are unique for each respondent in a survey, but the same respondent completing multiple surveys will have different id’s for each survey

`answer(question_id, [item_id], [stimulus_id])`Returns the responses in the question specified by `question_id`. `item_id` is an optional input to retrieve answers to an item in the question. `stimulus_id` is an optional input to retrieve answers to a particular stimulus in a monadic block.`answer(1559253)`
`bottom(question_id, limit, [stimulus_id])`Returns the N items the respondent placed at the bottom of the list in the question specified by `question_id`. N is determined by `limit`.`bottom(1549283, 2)`
`top(question_id, limit, [stimulus_id])`Returns the N items the respondent placed at the top of the list in the question specified by `question_id`. N is determined by `limit`.`top(1549283, 2)`
`count(question_id, [stimulus_id])`Returns the number of choices selected by respondents in the question specified by `question_id`. Function only works with multiple choice questions.`count(1559253)`
`npsStatus(question_id, [stimulus_id])`Returns the net promoter status (detractor, neutral, promoter) for Net Promoter Score questions. Specify question using `question_id``npsStatus(1557233)`