if vs switch statement | XM Community
Skip to main content
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()s0];
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 == 😎 {
Qualtrics.SurveyEngine.setEmbeddedData('Sign' , '$');
}
else if (answer == 9) {
Qualtrics.SurveyEngine.setEmbeddedData('Sign' , '$');
}
}, 100);
}
});
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.

Leave a Reply