(Using JS) Bringing participant's input data and comparing it against the random number generator. | XM Community
Skip to main content

Hello All,
I am trying to compare participant's input data, compare it against the number that is randomly generated, and show different messages depending on the size.
I have created a JS code that looks as below but I can't really find a reason why it does not work. I have tried it so many different ways but I keep getting an error message that there is an unexpected token {.

Qualtrics.SurveyEngine.addOnReady(function()
{
if (${e://Field/RandomAssetNo} > ${q://QID130/ChoiceTextEntryValue}) {
"Your earnings will be 0";
} else {
"Your earnings will be (${e://FieldRandomAssetNo - ${q://QID130/ChoiceTextEntryValue)";  
});

My two questions would be:
(1) Can I bring participant inputted values and randomly generated number like above?
(2) What else wrong am I doing with the code there?

I would appreciate any of your help!
Thanks!

Instead of JS, we can also use branch logic in the survey flow to determine the greater number.


Hello Rondev,
Thank you for response!
What I am trying to do with my JS is, compute a payoff for participants. For example, deduct two numbers and multiply $0.10. Is that also possible in the branch logic as well?
Thanks in advance!


https://www.qualtrics.com/community/discussion/comment/31577#Comment_31577Yes, we can use math operation for this


Thanks for your help! I guess I need to use if else function though. Do you think that's possible with math operation as well?
Thanks!


Based on the JS logic in question, in survey flow you need to have following setup:
Earnings = $e{ e://FieldRandomAssetNo - q://QID130/ChoiceTextEntryValue }
Branch Logic: ${e://Field/RandomAssetNo} > ${q://QID130/ChoiceTextEntryValue}
Earnings = 0


Hello Rondev,
That is brilliant! Instead of using JS, I will try using the branch logic (which I did).
However, I tried setting it up in my Qualtrics Survey; however, it skips the Branch If page. I have set it up as below. Do you happen any clue why it happens?
image.png
Thanks!


https://www.qualtrics.com/community/discussion/comment/31600#Comment_31600You need to select question from the first dropdown in branch logic


Leave a Reply