Embedded Data in Javascript to count overall number of responses to a question in a loop | XM Community
Skip to main content

Need some help. I'd like to count the number of responses to a certain multiple choice question that is part of a loop. I can get the number of responses to the question from the following:
var item_count= "${q://QID1/SelectedChoicesCount}";
I add embedded data: DSCOUNT to the survey flow at the start of the survey, and intitialize that to 0
But I am unable to operate on that as a number
var DSCount = Qualtrics.SurveyEngine.getEmbeddedData('DSCOUNT');
var item_count= "${q://QID1/SelectedChoicesCount}";
DSCount = DSCount + item_count;
Qualtrics.SurveyEngine.setEmbeddedData( 'DSCOUNT', DSCount); 

Any suggestions?
Thanks in advance!
Cindy

Have you tried the built in functions to do the calculations?
Math Operations (qualtrics.com)


The calculations need to be done outside of the survey flow. Using js inside of a large loop and merge block.


The embedded variables are always returned as strings, so based on your current code, you are just concatenating them. If you change to the following, it should start working properly:
var DSCount = Number(Qualtrics.SurveyEngine.getEmbeddedData('DSCOUNT'));
var item_count= Number("${q://QID1/SelectedChoicesCount}");


https://www.qualtrics.com/community/discussion/comment/34054#Comment_34054Thank you so much! That worked perfectly!


Hello,
I'm trying to do something very similar to the OP. I need to count the number of "yes" responses to a question in a loop. How could I go about modifying the above code to fit my needs. Also, would this code be added to addOnLoad or addOnReady?
Thanks!


Leave a Reply