The f function returns in fact different objects for different question types. These objects have some properties and methods in common, but some are specific to questions of a specific type.
Grid, multi, open text list, numeric list and ranking questions are questions with one or more variables that are defined in the same form. They are called compounds. They can be accessed using the f function in the normal manner, but the object returned is more complex than objects returned from other question types. You can refer directly to each of their elements using syntax that is similar to that of an array:
JScript example:
x[code]
JavaScript example:
x.item(code)
x represents a variable holding an instance of an object returned from calling the f function on a compound. code is a string representing the code from the answer list of the compound. An example is referring to the element with code "1" in a multi question with question ID q2:
JScript example:
f("q2")["1"]JavaScript example:
f("q2").item("1")Using a Variable instead of Repeated Calls on the f Function
Once more, back to the example with hours and weekdays where we validate the number of hours per day.
Figure 1 - Validating the number of hours per day
In our previous solution for validation of this question (go to while Statement for more information) we looped through the weekdays. For each day, we called the f function for three questions q2, q3 and q4:
JScript example:
sum = Sum(f("q2")[code],f("q3")[code],f("q4")[code]);JavaScript example:
sum = Sum(f("q2").item(code,f("q3").item(code),f("q4").item(code));Now, instead we can define three variables sleep, work and leisure to hold the form objects:
JScript example:
var sleep = f("q2");
var work = f("q3");
var leisure = f("q4");
var codes = sleep.domainValues(); // array with all codes
var i : int = 0;
var correctSum : Boolean = true;
while(i<codes.length && correctSum) //iterate through codes (rows)
{
var code = codes[i]; //current code
//calculate sum:
var sum : int = Sum(sleep[code],work[code],leisure[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 "+sleep[code].label()+" is "+sum+".");
}JavaScript example:
var sleep = f("q2");
var work = f("q3");
var leisure = f("q4");
var codes = sleep.domainValues(); // array with all codes
var i = 0;
var correctSum = true;
while(i<codes.length && correctSum) //iterate through codes (rows)
{
var code = codes[i]; //current code
//calculate sum:
var sum = Sum(sleep.item(code),work.item(code),leisure.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 "+sleep[code].label()+" is "+sum+".");
}The way the script was initially set up you could have as much as 21 calls (3*7=21) on the f function inside the loop. We have now reduced this to 3 function calls.