Loop&Merged choice based on matrix question answer

ptipptip Community Member Qubie ✭
edited July 2019 in Custom Code

Loop&Merged choice based on matrix question answer

Hi there,

I want to create a random choice based on a matrix question, which looks the following (simplified - the real questions has way more rows):

1.) The choices that I want to loop through several times should present the rows selected as A vs. selected rows as B (and should not present the same selections again) (Edit: to simplify things i decided to go for selection with replacement, so any pair random pair of A and B should be presented).

2.) In case there is no more rows A selected, should present two (but not the same) B and vice versa.

Any help highly appreciated!

Tagged:

Answers

  • TomGTomG Raleigh, NCCommunity Member Wizard ✭✭✭✭✭

    Pipe the selected answers for A and B into your web service or JavaScript where you will parse those lists and create random pairs. Return those pairs as embedded variables (e.g., L1_A, L1_B, L2_A, L2_B, etc.). Pipe those embedded variables into your loop and merge (e.g., Loop 1/Field 1 = ${e://Field/L1_A}, Loop1/Field 2 = ${e://Field/L1_B}, Loop2/Field1 = ${e://Field/L2_A}, etc.) There should be as many loops as there are rows in your matrix. Also, return the number of pairs as an embedded variable. Create a text entry question before your loop and pipe in the number of pairs as the default answer and hide it with JavaScript. Base your loop off the hidden question, so it loops the correct number of times.

    If you need to show the entered text for Row3 in your loop questions, pipe the entered text value into your randomization script and and do the replacement there so the entered text value is returned as an embedded variable value where appropriate.

  • ptipptip Community Member Qubie ✭

    Hi TomG,
    thank you for the answer. I tried to do as you said, however, I am struggeling to get it working.

    /*Pipe text from answer*/
        var A_str = "${q://QID1/ChoiceGroup/SelectedChoicesForAnswer/1}"
        var B_str = "${q://QID1/ChoiceGroup/SelectedChoicesForAnswer/2}"
    /*Parse/split text into array of strings*/
        var A_choices = A_str.split(",")
        var B_choices = B_str.split(",")
    /*Get a random element in the array of strings for A and B*/
    /* still need to figure out how; define var random_A_choice, var random_B_choice*/
    
    /*embed the element into a embedded data as good_choice/bad_choice*/
        setEmbeddedData("A_choices", random_A_choice)
            setEmbeddedData("B_choices", random_B_choice)
    

    I fail to even get the embedded data to display. I used the following to display it in the preview:
    ${e://Field/A_choices}
    however, it does not show anything -it is just blank.

    (please note: I decided to allow for selection with replacement to simplify things)

    @TomG said:
    Pipe the selected answers for A and B into your web service or JavaScript where you will parse those lists and create random pairs. Return those pairs as embedded variables (e.g., L1_A, L1_B, L2_A, L2_B, etc.). Pipe those embedded variables into your loop and merge (e.g., Loop 1/Field 1 = ${e://Field/L1_A}, Loop1/Field 2 = ${e://Field/L1_B}, Loop2/Field1 = ${e://Field/L2_A}, etc.) There should be as many loops as there are rows in your matrix. Also, return the number of pairs as an embedded variable. Create a text entry question before your loop and pipe in the number of pairs as the default answer and hide it with JavaScript. Base your loop off the hidden question, so it loops the correct number of times.

    If you need to show the entered text for Row3 in your loop questions, pipe the entered text value into your randomization script and and do the replacement there so the entered text value is returned as an embedded variable value where appropriate.

  • TomGTomG Raleigh, NCCommunity Member Wizard ✭✭✭✭✭

    You have errors in your JS:
    1. End statements with ; (semi-colon)
    2. Split pipe choices with ", " (a comma followed by a space)
    3. Use Qualtrics.SurveyEngine.setEmbeddedData()

    Use your browser's console to debug.

Sign In to Comment