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

Embedded Data in Javascript to count overall number of responses to a question in a loop

  • February 1, 2021
  • 5 replies
  • 418 views

cindyduggan
Forum|alt.badge.img+1

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

Best answer by ahmedA

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}");

View original

5 replies

pogi
Level 2 ●●
Forum|alt.badge.img+14
  • Level 2 ●●
  • 151 replies
  • February 2, 2021

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


cindyduggan
Forum|alt.badge.img+1
  • Author
  • 4 replies
  • February 2, 2021

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


Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2028 replies
  • Answer
  • February 2, 2021

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}");


cindyduggan
Forum|alt.badge.img+1
  • Author
  • 4 replies
  • February 2, 2021

Forum|alt.badge.img+3

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