Math calculations (JavaScript) not showing up in Qualtrics survey | XM Community
Solved

Math calculations (JavaScript) not showing up in Qualtrics survey

  • 13 April 2019
  • 7 replies
  • 72 views

I would like to use survey responses from a table of household members to calculate the total number of adults, kids and seniors. I have written javascript code to make the calculations and added this to a second block of the survey. I can read in the values entered but cannot get the calculations to show up.
This is how it works. First the respondent enters in the names, ages and gender of the respondents (morador is household member)
!

The next question (in a new block) includes the javascript and pipes in the embedded text and answers to individual survey questions.
!

The indivudal age values show up, suggesting that the data are being read, but the javascipt calculations do not. This is what I see (notice the ages are listed, but the calcualtions are blank):
!

This is my javascript (I tried it in the Onload and OnReady sections – neither works)
Qualtrics.SurveyEngine.addOnload(function()
{
// These lines read in the entered data on age

let age1= "${q://QID8%232/ChoiceTextEntryValue/1/1}";
let age2= "${q://QID8%232/ChoiceTextEntryValue/2/1";
let age3= "${q://QID8%232/ChoiceTextEntryValue/3/1}";

// create age variables
let adults = 0;
let kids = 0;
let seniors = 0;
if (age1 > 17 && age1 < 64) { adults++;} else if (age1 <= 17) { kids++;} else {seniors++;}
if (age2 > 17 && age2 < 64) { adults++;} else if (age2 <= 17) { kids++;} else {seniors++;}
if (age3> 17 && age3< 64) { adults++;} else if (age3 <= 17) { kids++;} else {seniors++;}

let family=adults+kids+seniors

Qualtrics.SurveyEngine.setEmbeddedData("adults",adults);
Qualtrics.SurveyEngine.setEmbeddedData("kids",kids);
Qualtrics.SurveyEngine.setEmbeddedData("seniors",seniors);
Qualtrics.SurveyEngine.setEmbeddedData("family", family);

});

And, to confirm, the embedded text is in the survey flow:
!

Any suggestions on what is wrong here? I tried entering in the ages as text (to add to a box as noted above) and from a drop down menu. Neither works.
Thanks for any suggestions!
icon

Best answer by WaterSampler 14 April 2019, 15:40

View original

7 replies

Badge +1
Is your embedded data block before the block with the javascript? If not, move it so that the embedded variables are created first.

Put this line before you set the embedded variables:
alert('adults = '+adults);
This will tell you if adults is being calculated correctly.

It could be that the ages are being considered to be text instead of numbers. This would prevent the if/else logic from working correctly. Try:
let age1= parsInt( "your question link here" );

Sorry about the "your question link here" i cannot post the code for some reason.

Also, the code as posted uses 17 as the kid/adult threshold but your question has 15. The same for seniors (65 vs. 59).
WaterSampler: Thanks for continuing to work with me on this. This worked well in my test survey. Once I moved this to my real survey I came across new problems.

You stated: Is your embedded data block before the block with the javascript? If not, move it so that the embedded variables are created first.

I am not sure what you mean by this. My survey has two questions. Q1: table with family members. Q2: table with the summary stats. My javascript is below. I have tried adding this to Q1 and Q2 (neither works). Are you saying that I should split the code below between the two questions?

This is where I have it in Q2 (in the Onload section). I also tried the onReady section.

Qualtrics.SurveyEngine.addOnload(function()
{

let age1= parsInt("${q://QID8%232/ChoiceTextEntryValue/1/1}");
let age2= parsInt("${q://QID8%232/ChoiceTextEntryValue/2/1}");
let age3= parsInt("${q://QID8%232/ChoiceTextEntryValue/3/1}");

// create age variables
let adults = 0;
let kids = 0;
let seniors = 0;
let family =0;

if (age1 > 17 && age1 < 64) { adults++;} else if (age1 <= 17) { kids++;} else {seniors++;}
if (age2 > 17 && age2 < 64) { adults++;} else if (age2 <= 17) { kids++;} else {seniors++;}
if (age3> 17 && age3 < 64) { adults++;} else if (age3 <= 17) { kids++;} else {seniors++;}

family=adults+kids+seniors

Qualtrics.SurveyEngine.setEmbeddedData("adults",adults);
Qualtrics.SurveyEngine.setEmbeddedData("kids",kids);
Qualtrics.SurveyEngine.setEmbeddedData("seniors",seniors);
Qualtrics.SurveyEngine.setEmbeddedData("family", family);
});

Also note that I tried the "parsInt" This did not work. (And, yes I know the numbers differ between what I state on the page and what I have in the code. I'll fix this once I have the code working. When I use the alert, I get a blank - so I know this is not being calculated correctly. I used the output in question 2 to make sure that I am grabbing the correct variable. Any other suggestions?
Badge +1
The block where you declare the embedded variables (in survey flow) should be before the block where you use the embedded. I usually make it the first block in the flow, that way the embedded variables are always there when needed. Click the word "move" and drag the block upwards.

Your debug output showing the values of age1, age2, age3 indicates that the values from the question are being read as you surmised.

The blank output for the alert indicates that the values are not being calculated. Try doing an alert with the age variables.

Also, try replacing the let declaration with a var declaration. See https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var for an example. Then try putting alerts in as before.

Try moving Q2 to a new block or inserting a page break before it. My current survey also has a summary block, but it does a summary over 5 blocks, where some blocks are not available to the end user. The summary block is on its own and works fine.
Badge +1
Also, the family statement should be terminated with a semicolon.
You were correct. Including an empty block with the javascript before the table addressed the problem. Next I would like to include the adults/children/seniors in the table so that in total it includes all the adults/children/seniors by gender and then in total. Do you think this code would correctly sum the women (Femino=Female):

// reading in gender
let gender1= ("${q://QID8%233/ChoiceGroup/SelectedAnswers/1}");

// create female_ age variables
let f_adults = 0;
let f_kids = 0;
let f_seniors = 0;

///female adults, kids and seniors - I am not sure if this is codded correctly - should sum all the female family members
if(age1 <= 15&&gender1==='Feminino'){ f_kids++;}else if(age1>15 && age1 < 60&&gender1==='Feminino' ){ f_adults++; } else if(age1>60 && gender1==='Feminino'){f_seniors++} else {0};
Badge +1
Your code will be more readable if you nest your conditional tests:
if (gender1 == 'Feminino') {
female 1 age processing here...
} else if (gender1 == 'Masculino') {
male 1 age processing here...
}

For example, your sample code will skip counting the first person if the response is female and the age is 60. The final age comparison is a '>' instead of '>='.

Just a personal preference, but when I make complex comparisons, the bugs take longer to find.

Glad it is working for you now.
Thanks!

Leave a Reply