JavaScript/embedded data calculation sometimes works, sometimes is zero | XM Community

JavaScript/embedded data calculation sometimes works, sometimes is zero

  • 23 November 2022
  • 2 replies
  • 113 views

Userlevel 2
Badge +6

Hello community,
I'm nearly done implementing a research questionnaire in Qualtrics, and I'm at the very final stages where I would like to calculate some totals. I am calculating some of the totals in JavaScript and setting them in embedded data, and others are completely calculated in embedded data using Qualtrics math. For percentages that need to be rounded, I calculate the unrounded percentage in JavaScript and then use Qualtrics rounding in embedded data to finalize them (rounding in JavaScript was not reliably giving me the results I wanted).
All of my totals seem to be calculating correctly except for one set, and I cannot figure out why. For these calculations, I'm getting 8 embedded variables, global exposure estimates for 4 languages and cumulative exposure estimates for 4 languages. I have checked and these are parsing correctly.
Once I have those, I need to set an overall estimate, which is determined based on whether the participant meets a monolingualism criterion or not. If they are monolingual, then we just set the cumulative exposure as the overall exposure. If they are not monolingual, then we must average the global and cumulative estimates.
The embedded data variables for the 4 overall exposure estimates are set in the block before the JavaScript calculations, and the type is set to 'Number'. The JavaScript calculations are below. In the block following, I use Qualtrics math to round the estimates and get percentages.
When I test my questionnaire, all my calculations (which are more or less calculated in a similar way) are working correctly, and sometimes the overall estimates are correct, but about 30% of the time they all become zero.
Can anyone see anything obvious I am doing incorrectly that would explain this result?
Thanks very much for any insight!
Setting embedded data variables before the calculations block:
image.pngThe JavaScript:
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
/*Place your JavaScript here to run when the page is submitted*/

// This section calculates the overall estimate by averaging the cumulative percentage with the global estimate
// MONOLINGUAL EXCEPTION: do not average if cumulative is >90% for one language, instead just use cumulative

var l1glob = parseFloat("${e://Field/l1_global_exp}");
var l2glob = parseFloat("${e://Field/l2_global_exp}");
var l3glob = parseFloat("${e://Field/l3_global_exp}");
var l4glob = parseFloat("${e://Field/l4_global_exp}");

var l1_cumu = parseFloat("${e://Field/l1_cumu_exp}");
var l2_cumu = parseFloat("${e://Field/l2_cumu_exp}");
var l3_cumu = parseFloat("${e://Field/l3_cumu_exp}");
var l4_cumu = parseFloat("${e://Field/l4_cumu_exp}");

if (l1_cumu > .9 || l2_cumu > .9 || l3_cumu >.9 || l4_cumu > .9) {
var mono = 1;
} else {
var mono = 0;
}

if (mono == 0) {
var l1_overall = (l1glob + l1_cumu) / 2;
var l2_overall = (l2glob + l2_cumu) / 2;
var l3_overall = (l3glob + l3_cumu) / 2;
var l4_overall = (l4glob + l4_cumu) / 2;
Qualtrics.SurveyEngine.setEmbeddedData("mono_exception_text", "\\n\\nThe overall exposure is calculated by averaging the cumulative exposure with the global estimate.\\n\\n");
} else if (mono == 1) {
var l1_overall = l1_cumu;
var l2_overall = l2_cumu;
var l3_overall = l3_cumu;
var l4_overall = l4_cumu;
Qualtrics.SurveyEngine.setEmbeddedData("mono_exception_text", "\\n\\n*Monolingual exception: the cumulative average was used for the overall estimate because one language had more than 90% exposure.\\n\\n");
}

Qualtrics.SurveyEngine.setEmbeddedData("l1_overall_exp", l1_overall);
Qualtrics.SurveyEngine.setEmbeddedData("l2_overall_exp", l2_overall);
Qualtrics.SurveyEngine.setEmbeddedData("l3_overall_exp", l3_overall);
Qualtrics.SurveyEngine.setEmbeddedData("l4_overall_exp", l4_overall);
Qualtrics.SurveyEngine.setEmbeddedData("mono_exception", mono);
});
The Qualtrics math after the calculations:
image.png


2 replies

Userlevel 5
Badge +25

Hi hilaryk,
Are you able to reproduce the conditions when you get the result of 0? Ideally given the same input, it should produce the same output.
Also, if you don't want to use Qualtrics math, you might get the behaviour you want from the following:
l1_overall.toFixed(2); //rounds to two decimals

Userlevel 2
Badge +6

Thanks for replying, bgooldfed ! I was not able to reliably reproduce the conditions with the zero result, but I think I fixed the problem. On the calculation question, I have a hidden next button and a delay before automatically submitting the page. The calculations were being done in JavaScript onPageSubmit, but I have since placed them in onReady and now I am no longer getting any zeros (so far). Perhaps the auto advancing was interfering with the calculations in some way.

Leave a Reply