Select to view content in your preferred language

# Peculiar behaviour when summing up questions

198
1
a month ago
New Contributor II

Hi,

I stumbled across some peculiar behaviour when trying to sum some yes/no questions. The 7 yes/no questions use a select_one yes_no with 1=Yes and 0=No.

A total questions has a calculation that sums the results as

\${question1}+\${question2}+\${question3}+\${question4}+\${question5}+\${question6}+\${question7}

If I start ticking Yes from the top (question 1) to the bottom (question 7) the summation works as expected. If I start at the bottom (question 7) and go to the top (question 1), the total interprets the answer as strings and start concatenating the, so ,'1', '11', '111','1111','11111', '111111' until I tick the top question. Then the total changes to 7.

If I add another Total that converts the answers to integers before summing, I get a different behaviour. The total only gets calculated after all questions have been answered.

Attached are 2 videos showing the summations plus the XLSForm.

If you want to be sure that the calculations work, no matter what order they are ticked off, the following calculations should always work:

Numbers

if(string-length(\${question1})>0,int(\${question1}),0)+if(string-length(\${question2})>0,int(\${question2}),0)+if(string-length(\${question3})>0,int(\${question3}),0)+if(string-length(\${question4})>0,int(\${question4}),0)+if(string-length(\${question5})>0,int(\${question5}),0)+if(string-length(\${question6})>0,int(\${question6}),0)+if(string-length(\${question7})>0,int(\${question7}),0)

Strings:

if(string-length(\${question1})>0,\${question1},0)+if(string-length(\${question2})>0,\${question2},0)+if(string-length(\${question3})>0,\${question3},0)+if(string-length(\${question4})>0,\${question4},0)+if(string-length(\${question5})>0,\${question5},0)+if(string-length(\${question6})>0,\${question6},0)+if(string-length(\${question7})>0,\${question7},0)

It's not a big thing, but something to keep in mind.