Arithmetic Operators
Operator |
Description |
+ |
Addition |
- |
Subtraction or unary negation |
* |
Multiplication |
/ |
Division |
% |
Modulus (the remainder of dividing two integers). |
++ |
Increment and then return value (or return value and then increment) |
-- |
Decrement and then return value (or return value and then decrement) |
Some of these may need some explanation:
Modulus gives the remainder of dividing two integers. Examples:
7%2gives 1 because 7/2 = 3 with a remainder of 1.
6%2 gives 0 because 6/3=2 with no remainder.
++ and --:x++ and ++x both result in 1 being added to the value of x. x-- and --x both results in 1 being subtracted from x. They differ in what value is returned - the value before or after the increment/decrement. ++x and --x return the value after the increment/decrement. x++ and x-- return the value before the increment/decrement.
The distinction between x++ and ++x is illustrated in these two coding examples:
|
x = 3 Result: x = 4 and y = 4 |
x = 3 Result: x = 4 and y = 3 |
In the first example, x is increased by 1 before the value is returned and stored in y. In the second the value of x is returned and stored in y first, and then x is increased by 1. We recommend that you be very careful when using these operators.
Logical Operators
Operator |
Description |
&& |
logical and |
|| |
logical or |
! |
logical not |
Comparison Operators
Operator |
Description |
== |
Equal |
=== |
Strictly equal (without type conversion) |
!= |
Not equal |
!== |
Strictly not equal (without type conversion) |
< |
Less than |
<= |
Less than or equal |
> |
Greater than |
>= |
Greater than or equal |
Expressions with comparison operators evaluate to true or false.
The difference between equal and strictly equal(==/===): When using == the scripting engine automatically does a type conversion according to the types' order of precedence. For example, in the expression x == y, where x is the string '1' and y is the number 1, the value of y will be converted to the string '1' and the expression will return true. However, in the expression x === y (x strictly equal to y) this conversion will not be done, hence the expression will return false, because the operands have different types.
The same applies to not equal/strictly not equal (!=/!==).
String Operators
Operator |
Description |
+ |
String concatenation |
Strings can be joined with the string concatenation operator +:
"For"+"sta"will return
"Forsta"
Assignment Operators
Operator |
Description |
= |
Sets the variable on the left of the = operator to the value of the expression on its right. |
+= |
Increments the variable on the left of the += operator by the value of the expression on its right. When used with strings, the value to the right of the += operator is appended to the value of the variable on the left of the += operator. |
-= |
Decrements the variable on the left of the -= operator by the value of the expression on its right. |
*= |
Multiplies the variable on the left of the *= operator by the value of the expression on its right. |
/= |
Divides the variable on the left of the /= operator by the value of the expression on its right. |
%= |
Takes the modulus of the variable on the left of the %= operator using the value of the expression on its right. |
The following table explains the operators +=, -=, *=, /= and %=.
Operator |
Equivalent to |
x += y |
x = x+y |
x -= y |
x = x-y |
x *= y |
x = x*y |
x /= y |
x = x/y |
x %= y |
x = x%y |
+= can be used for text concatenation as well as arithmetic addition:
x = "Confirm";
x += "it";
will result in x being equal to Confirmit.
Conditional Expression Ternary Operator
This operator takes three operands (that is why it is called ternary):
condition ? value1 : value2The first item is a condition that evaluates to either true or false. If the condition evaluates to true, value1 is returned. If the condition evaluates to false, value2 is returned.
Response Piping from a Single Question with Other Specify
If you have text substitution in Forsta Plus and want to pipe in the response to a single question with an item with the Other property set, you probably want to pipe in the response in the Other text box instead of the answer text (which might be something like "Other, please specify:").
Say you want to pipe in the response to the single question musical where there is one item in the answer list with code 98 that has the Other property set.
Figure 1 - Selecting the Other property set
In the text field of the question or info where you want to pipe in the response to the musical question, you can use this code:
^f("musical").get() == "98" ? f("musical_98_other") : f("musical")^f("musical").get()
will return the code of the answer to musical (go to get and set for more information). If this is equal to 98, the answer from the Other text box should be piped in (f("musical_98_other")). If not, use normal text substitution with f("musical") so that the answer text will be piped in.
Figure 2 - Code of the answer to musical
Replacing "NO RESPONSE" in Response Piping
When there is no answer to a question, text substitution will give the text "NO RESPONSE" (different texts in other languages than English). So if the respondent for example does not answer on a not required single question income, response piping with
Income: ^f("income")^will give this result:
Figure 3 - Income response piping
If you instead want an empty string (i.e. no text at all) to appear when the question is not answered, use this expression in the question where you want to pipe in the answer:
Income: ^f("income").toBoolean() ? f("income") : ""^If there is an answer to Income, f("income").toBoolean() will return true and f("income") will be used for text substitution. If there is no answer, an empty string will be returned.
Figure 4 - Empty string returned
Note: Expressions inside ^ (carets) in question text fields, title fields and answer/scale lists in Forsta Plus must always return strings. You cannot run scripting statements inside ^'s, however you can call a function that returns a string.
Conditional Code Masking
It is also possible to use this operator in a code mask so a different code mask can be used dependent on a condition. For example, if you want males (code 1) to be given different answer alternatives than women (code 2), you can base the code mask on the "gender" question like this:
f("gender")=="1" ? nset(5) : set('1','2','4')If the condition is evaluated as true(=male) then a code mask of 1-5 would be used; if the condition is evaluated as false (=female) then a code mask of 1, 2 and 4 would be used.
Figure 5 - Conditional code masking example