Sum
Sum( arguments )Sum returns the sum of its arguments. Valid argument types are numbers, strings, arrays or any kind of object. Use comma to separate the arguments.
Validating Sum on a Numeric List Question
Here is an example of a numeric list question, in which the respondents are asked to apply percentages that should sum up to 100. You can use the Auto sum property to have the sum calculated and displayed directly below the respondent's answers.
Figure 1 - Example of a numeric list question
The question id of the numeric list question is percentage. In properties total digits is set to 3, scale to 0, and lower and upper limit to 0 and 100, so that the system provided validation makes sure that only integers between 0 and 100 is allowed. To validate that the answer is equal to 100, you can use the Force sum of answers setting in the question properties, or use the following code in the validation code field of the question:
JScript example:
var sum : int = Sum(f("percentage").values());
if(sum!=100)
{
RaiseError();
SetQuestionErrorMessage(LangIDs.en, "Your numbers add up to " + sum + ". Please make sure that the numbers add up to 100.");
}
JavaScript example:
var sum = Sum(f("percentage").values());
if (sum != 100) {
RaiseError();
SetQuestionErrorMessage(LangIDs.en, "Your numbers add up to " + sum + ". Please make sure that the numbers add up to 100.");
}
Validating Sums in a 3D Grid with the Sum Function
Let us go back to the example with the validation script that checked that the respondent answered a total of 24 hours for each day (see the examples in while statement, do while statement, and break statement for details).
Figure 2 - Validation script example
In the JScript example we used the expression:
sum = f("q2")[code].toNumber()+f("q3")[code].toNumber()+f("q4")[code].toNumber();
to add up the number of hours. Instead we could use the Sum function, like this:
sum = Sum(f("q2")[code],f("q3")[code],f("q4")[code]);
Similar, in the JavaScript example we used the expression:
sum = f("q2").item(code).toNumber()+f("q3").item(code).toNumber()+f("q4").item(code).toNumber();
to add up the number of hours. Instead we could use the Sum function, like this:
sum = Sum(f("q2").item(code),f("q3").item(code),f("q4").item(code));
Notice that the Sum function automatically converts the values to numeric, so we do not need to use the toNumber method.
Then the entire validation code will look like this, if we use the solution with the while loop:
JScript example:
var codes = f("q2").domainValues(); //array with all codes
var i : int = 0;
var correctSum : Boolean = true; //Boolean variable. Will be set to false when a sum is not correct
while(i<codes.length && correctSum)
{
var code = codes[i]; //current code
//calculate the sum for one row:
var sum : int = Sum(f("q2")[code],f("q3")[code],f("q4")[code]);
if(sum != 24)
{
correctSum = false;
}
i++;
}
if(!correctSum)
{
RaiseError();
SetQuestionErrorMessage(LangIDs.en,"Please make sure that the total number of hours for each day equals 24. Currently the sum for "+f("q2")[code].label()+" is "+sum+".");
}
JavaScript example:
var codes = f("q2").domainValues(); //array with all codes
var i = 0;
var correctSum = true; //Boolean variable. Will be set to false when a sum is not correct
while(i<codes.length && correctSum)
{
var code = codes[i]; //current code
//calculate the sum for one row:
var sum = Sum(f("q2").item(code),f("q3").item(code),f("q4").item(code));
if(sum != 24)
{
correctSum = false;
}
i++;
}
if(!correctSum)
{
RaiseError();
SetQuestionErrorMessage(LangIDs.en,"Please make sure that the total number of hours for each day equals 24. Currently the sum for "+f("q2")[code].label()+" is "+sum+".");
}
Count
Count( arguments )Count returns the number of arguments. Valid argument types are numbers, strings, arrays or any kind of object. Use comma to separate the arguments. An array will be split into its elements, so used on an array Count will return the number of elements in the array.
Finding the Number of Answers on Three Multi Questions
We have three multi questions; officesa, officesb and officesc. This could for example be a question in an employee survey for an international company with offices all around the world, where we want to split the list of offices on three different questions on three different pages.
Figure 3 - officesa Multi Question
Figure 4 - officesb Multi Question
Figure 5 - officesc Multi Question
If you want to find the number of items answered on all three questions combined, for example for use in a condition to ask some further question(s) only if the respondent has answered more than one office, you can use the Count function:
Count(f("officesa").categories(),f("officesb").categories(),f("officesc").categories()) > 1If the respondent has picked 2 offices on q1, 1 office on q2 and 3 offices on q3, the result of this function call will be the value 6 (2+1+3).
Average
Average( arguments )Average returns the average (mean) of its arguments. Valid argument types are numbers, strings, arrays or any kind of object. Use comma to separate the arguments.
Calculating Averages on 3 Numeric List Questions in a 3D Grid
Again referring to the example with the hours and the weekdays, let us say we want to set three hidden numeric questions sleep, work and leisure with the daily average for each of them.
We use the Average function:
f("sleep").set(Average(f("q2").values()))
f("work").set(Average(f("q3").values()))
f("leisure").set(Average(f("q4").values()))(The 3D grid consisted of three numeric list questions, q2 for sleep, q3 for work and q4 for leisure). The hidden question sleep for instance, will here be set to 8 since 56/7 is 8.
Max and Min
Max( arguments )Min( arguments )Max returns the maximum (the largest value) of its arguments. Min returns the minimum (the smallest value) of its arguments. Valid argument types are numbers, strings, arrays or any kind of object. Use comma to separate the arguments.
Finding Maximum and Minimum Values on Numeric List Questions
Once again, referring to the "hours and weekdays", let us say we want the maximum number of work hours in the week (stored in a hidden question maxwork) and the minimum number of hours sleep in the week (stored in a hidden question minsleep):
f("maxwork").set(Max(f("q3").values()))
f("minsleep").set(Min(f("q2").values()))In the example, maxwork will get the value 12 and minsleep the value 5.
Range
Range has two alternatives:
InRange(arg,min,max)InRange returns true if arg is within the range (min, max) (inclusive).
InRangeExcl(arg,min,max)InRangeExcl returns true if arg is within the range (min, max) (exclusive).
Building a Condition on a Range of Codes
You may need your conditions to work for several codes within a range. For example, you may have an age question where this is a part of the answer list:
Figure 6 - Age question part of an answer list
If you wanted some questions to be sent only to people between the ages of 26 and 50, you could use a condition with an expression as follows:
f("age").toNumber() >= 4 && f("age").toNumber() <= 8but in this case it will be easier to use the InRange function:
InRange(f("age"),4,8)