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

(Using JS) Bringing participant's input data and comparing it against the random number generator.

  • November 2, 2020
  • 7 replies
  • 18 views

Forum|alt.badge.img

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!

7 replies

rondev
Level 6 ●●●●●●
Forum|alt.badge.img+22
  • Level 6 ●●●●●●
  • November 2, 2020

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


Forum|alt.badge.img
  • Author
  • November 2, 2020

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!


rondev
Level 6 ●●●●●●
Forum|alt.badge.img+22
  • Level 6 ●●●●●●
  • November 2, 2020

Forum|alt.badge.img
  • Author
  • November 2, 2020

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!


rondev
Level 6 ●●●●●●
Forum|alt.badge.img+22
  • Level 6 ●●●●●●
  • November 2, 2020

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


Forum|alt.badge.img
  • Author
  • November 3, 2020

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!


rondev
Level 6 ●●●●●●
Forum|alt.badge.img+22
  • Level 6 ●●●●●●
  • November 3, 2020

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