if vs switch statement | XM Community
Skip to main content
Solved

if vs switch statement

  • November 8, 2019
  • 1 reply
  • 36 views

Forum|alt.badge.img+1
Any reason that this code wouldn't work: Qualtrics.SurveyEngine.addOnload(function () { var that = this; that.questionclick = function (event,element) { setTimeout(function () { var answer = that.getSelectedChoices()[0]; switch(answer) { case 1: case 2: case 8: case 9: Qualtrics.SurveyEngine.setEmbeddedData('Sign' ,'$'); break; case 3: Qualtrics.SurveyEngine.setEmbeddedData('Sign' , '£'); break; case 4: Qualtrics.SurveyEngine.setEmbeddedData('Sign' , '€'); break; } }, 100); } }); But this codes does: Qualtrics.SurveyEngine.addOnload(function () { var that = this; that.questionclick = function (event,element) { setTimeout(function () { var answer = that.getSelectedChoices()[0]; if (answer == 1) { Qualtrics.SurveyEngine.setEmbeddedData('Sign' ,'$'); } else if (answer == 2) { Qualtrics.SurveyEngine.setEmbeddedData('Sign' , '$'); } else if (answer == 3) { Qualtrics.SurveyEngine.setEmbeddedData('Sign' , '£'); } else if (answer == 4) { Qualtrics.SurveyEngine.setEmbeddedData('Sign' , '€'); } else if (answer == 8) { Qualtrics.SurveyEngine.setEmbeddedData('Sign' , '$'); } else if (answer == 9) { Qualtrics.SurveyEngine.setEmbeddedData('Sign' , '$'); } }, 100); } });

Best answer by HPowell

Since I can't delete the post, it has to do with the fact the switch statement uses strict comparison while the if statement (==) doesn't. I didn't realize that the qualtrics method `getSelectedChoices()` returns a string and not an integer. Wrapping parseInt() i.e `parseInt(that.getSelectedChoices()[0], 10);` fixed the issue.

1 reply

Forum|alt.badge.img+1
  • Author
  • Answer
  • November 9, 2019
Since I can't delete the post, it has to do with the fact the switch statement uses strict comparison while the if statement (==) doesn't. I didn't realize that the qualtrics method `getSelectedChoices()` returns a string and not an integer. Wrapping parseInt() i.e `parseInt(that.getSelectedChoices()[0], 10);` fixed the issue.