conditional loop? | XM Community
Skip to main content
Solved

conditional loop?

  • September 17, 2019
  • 6 replies
  • 97 views

Hi all, I would appreciate any help in creating a conditional loop in Qualtrics. Here is what I am trying to do: I have a block of 40 questions, which I would like to present to participants randomly. I would like to repeat the presentation of this block until participants get 25 questions right. In this case, participants will progress in the next block. many thanks in advance

Best answer by TomG

Unfortunately, you can't use scoring in display logic. So, you'll have to use some JavaScript to keep a count of correct answers using the addOnPageSubmit function. Then you'll have to add display logic to each question in the block to only display it if the correct count is less than 25. Initialize correctCount to 0 in the survey flow before the block. Then add JS something like this to each question: ``` Qualtrics.SurveyEngine.addOnPageSubmit(function() { var correctChoice = "2" //change to match correct choice id if(correctChoice == this.getSelectedChoices()) { Qualtrics.SurveyEngine.setEmbeddedData("correctCount",parseInt("${e://Field/correctCount}")+1); } }); ```

6 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 17, 2019
Are 40 questions one question in a randomized Loop & Merge block or 40 separate questions in a randomized block?

  • Author
  • September 17, 2019
40 separate questions in a randomized block, thanks

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Answer
  • September 17, 2019
Unfortunately, you can't use scoring in display logic. So, you'll have to use some JavaScript to keep a count of correct answers using the addOnPageSubmit function. Then you'll have to add display logic to each question in the block to only display it if the correct count is less than 25. Initialize correctCount to 0 in the survey flow before the block. Then add JS something like this to each question: ``` Qualtrics.SurveyEngine.addOnPageSubmit(function() { var correctChoice = "2" //change to match correct choice id if(correctChoice == this.getSelectedChoices()) { Qualtrics.SurveyEngine.setEmbeddedData("correctCount",parseInt("${e://Field/correctCount}")+1); } }); ```

  • Author
  • September 18, 2019
this is very useful, thank you very much

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 18, 2019
I noticed the code above as missing an = in the if. I've updated it.

Hi TomG,
I am attempting something very similar to this in a quiz I am designing. However, I am a complete newbie to Qualtrics and Javascript and I need further guidance!
Basically, I have outlined a scenario and follow it with five different multiple choice questions, each with four potential answers. I do not want the participants to move onto the next block unless all five questions are answered correctly (i.e. the correctCount = 5).
Could you please tell me how I could alter your suggested JS code so I can identify the correct multiple choice answer for each question? Please note that my multiple choice answers are written as "You received a payoff of __ and a cost of __".
Thank you for your time.