Multicheck Counting Recipe
Scenario: You have a scored number question that scores based on the number of multi-checkbox responses that are checked. You want to write a rule to automatically fill this scored number question, but there is no rule to count checkboxes.
Example:
Q. 31:
Which of the following did the server do (check all that apply):
- Greet you at the table
- Handed you a menu right away
- Provided you with water without request
- Returned with correct selection
Q. 41 (hidden from shopper):
Scored Number Question, 1 point per checkbox (7 possible choices)
Because there is no rule to count multi-checkbox responses, you must check each possible combination of choices. For the example above:
ifQ(31) .notAnswered() .setValue(Q(41), 0); ifQ(31) .answered([1], [2], [3], [4]) .setValue(Q(41), 1); ifQ(31) .answered([1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]) .setValue(Q(41), 2); ifQ(31) .answered([1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]) .setValue(Q(41), 3); ifQ(31) .answered([1, 2, 3, 4]) .setValue(Q(41), 4);
Remember that .answered can take multiple values separated by a comma, which means "if any of these are selected":
// set value if answered 1 OR 2 OR 3 OR 4
ifQ(31) .answered(1, 2, 3, 4) .setValue(Q(41), 4)
However, if you add square brackets around a list of answer options, it means "if ALL of these (and ONLY these) are selected”:
// set value if answered 1 AND 2 AND 3 AND 4 and NOTHING else!
ifQ(31) .answered([1, 2, 3, 4]) .setValue(Q(41), 4)
It is important to understand the difference between the two rules above. The square brackets in the second rule are what allow you to ensure that Q(41) is set to 4 if and only if Q(31) has 4 answers chosen at the same time.
What about the rules above, that have both bracketed values, and commas between them?
// set value if answered (1 AND 2 AND 3) OR (1 AND 2 AND 4)
// OR (1 AND 3 AND 4) OR (2 AND 3 AND 4)
ifQ(31) .answered([1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]) .setValue(Q(41), 3);
Notice that we’ve put in every possible 3-answer combination. This ensures that any 3-checkbox response will trigger this rule.
From this explanation, you should be able to see that each one of those 5 rules in the set corresponds to a situation where a different number of items is chosen.
As you might imagine, because this technique requires that you consider every combination, this recipe becomes unwieldy when the number of checkboxes increases. Therefore, we recommend that this technique not be used when there are a lot of choices. It is better to separate each item as a Yes/No question to be scored separately, e.g.
- Did the server greet you at the table? Yes/No
- Did the server hand you a menu right away? Yes/No
- Did the server provide you with water without request? Yes/No
- Did the server return with correct selection? Yes/No