This article describes various methods of writing conditional formulas using JavaScript.

1. Mathematical operators and formulas

## Requirements

- A JavaScript variable, rule or QScript.
- You have read JavaScript Fundamentals.
- The main condition operators are as follows:

## Method

### 1. Mathematical operators and formulas

All the traditional mathematical operators (i.e., +, -, /, (, ), and *) work in JavaScript in the way that you would expect when performing math on variables:

(brandA + brandB + brandC) / daysInYear

All the standard mathematical functions are also available in JavaScript. For example:

`Math.abs(x)`

- Computes the absolute values of*x*(i.e. removes any negative sign).`Math.floor(x)`

– Rounds*x*down to the nearest whole number.`Math.ceil(x)`

– Rounds*x*up to the nearest whole number.`Math.max(x)`

– Finds the maximum value from*x*.`Math.min(x)`

– Finds the minimum value from*x*.

### 2. Boolean expression

A *Boolean expression* is an expression which evaluates to *true* or *false*. Results that are *true* are returned as values of 1 and* false* as 0 (i.e., this is a way to construct a binary variable).

For example, we have two numeric variables, *v1* and *v2*, and a text variable,* v3*:

`v1 != v2`

returns a 1 for observations where*v1*and*v2*are different, and a value of 0 when they are the same.`isNaN(v1)`

returns a 1 for observations in*v1*that have the value of NaN (missing data), otherwise it returns a 0. For text variables, use`v1 == ""`

`(v1 + v2) > 0`

returns a 1 if the sum of*v1*and*v2*is greater than 0, otherwise it returns a 0.- The below formula returns a 1 if
*v3*contains an @ character, otherwise it returns a 0. The*indexOf*function finds the position of the specified string, whereby -1 is equivalent to being missing and 0 means it is the first character. Note, we have to first define*v1*in order to append the*indexOf*function to it:

var v = v3;

v.indexOf("@")>-1

### 3. Using *if...else*

The* if* then *else* structure is one of the most common methods for writing conditions in programming languages. The following are examples of how this can be used in Displayr.

### Banding variables

Let's say we have a variable called *sumCola* that sums the number of cola drinks each respondent in our survey drank. We want to now split these respondents into segments based on their frequency. One option is to set up a numeric JavaScript variable that assigns a **1** for less than 5 drinks (*Light Drinker*), a **2** for 5 to 10 drinks (*Medium Drinker*), and a **3** for 10 or more (*Heavy Drinker*):

if (sumCola < 5) 1;

else if (sumCola >= 5 && sumCola <= 10) 2;

else if (sumCola > 10) 3;

else NaN;

We would then need to change **Properties > Structure** to **Nominal** and assign our segment labels via **DATA VALUES > Labels**.

Alternatively, you can create a text variable via JavaScript which instead returns the text response. Changing **Properties > Structure** to **Nominal** will let Displayr automatically set up the value labels when it converts to a categorical variable.

The same example would look like this:

if (sumCola < 5) "Light Drinker"; else if (sumCola >= 5 && sumCola <= 10) "Medium Drinker"; else if (sumCola > 10) "Heavy Drinker"; else "";

### Combining variables

Consider a situation where we’ve collected data for four countries: UK, France, Australia, and Japan. The data for each country was held in 4 separate data files and merged into a final file. Membership to a particular segment (market) is defined by a variable called `country`

. Each country’s data file had a specific weighting variable, so we now have four different weighting variables in the merged data file (`weight_UK`

, `weight_FR`

, `weight_AU`

, `weight_JP`

). When we do our analysis, we only want to deal with one weighting variable. So let’s combine them using JavaScript:

if (country == 1) weight_UK; else if (country == 2) weight_FR; else if (country == 3) weight_AU; else if (country == 4) weight_JP; else NaN;

### 4. The *if...else* shortcut

There is also a shortcut method of *if...else* that lets you write a condition in a single line. In the below example, the formula will return a **Yes** if *x* is greater than 1, otherwise a **No**:

x > 1 ? "Yes" : "No";

## See Also

How to Use JavaScript in Displayr

How to Work with JavaScript Arrays and Loops

How to Manipulate Strings Using JavaScript

How to Troubleshoot JavaScript Code in Displayr

## Comments

0 comments

Article is closed for comments.