Code for randomly printing words and updating embedded data field doesn't work | Experience Community
Skip to main content
Question

Code for randomly printing words and updating embedded data field doesn't work

  • Nov 5, 2021 3:45 PM UTC
  • 1 reply
  • 29 views

Forum|alt.badge.img

I'm using JS script to get qualtrics to pick randomly a word from a set of words and print it out on the screen using embedded data field. At the same time, I also populate another embedded data field with the words that have been picked so that subsequent questions will not print out the same words again.
Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place your JavaScript here to run when the page loads*/
    /* pass in the content of the embedded data field */
    var used = "${e://Field/used}";
    /* set an array of all the words to be randomly picked */
    var everything = ["hoodie","coffee maker","amusement park ticket","concert ticket"];

    if (used.split("").length == 0) { /* check if the read-in embedded data field is empty */
        /* pick a word from the array randomly */
        var good = everything[Math.floor(Math.random() * everything.length)];
        /* set the word to the embedded field called 'good', this will be piped into the question*/
        Qualtrics.SurveyEngine.setEmbeddedData("good", good);
        /* overwrite the embedded field 'used' */
        Qualtrics.SurveyEngine.setEmbeddedData("used", good);
        
        console.log(good);
        console.log(used);
    } else {
        
        var usedArray = used.split(",");
        /* make an array that only contains those words that haven't been picked */
        var newArray = everything.filter( function( el ) {
          return usedArray.indexOf( el ) < 0;
        } );
        
        var good = newArray[Math.floor(Math.random() * newArray.length)];
        
        /* update the array with used words */
        usedArray = usedArray.push(good);
        
        /* join the array with used words into a string, to update the corresponding embedded field */
        used = usedArray.join(",");
            
        Qualtrics.SurveyEngine.setEmbeddedData("good", good);
        /* overwrite the embedded field 'used' with the updated string of used words */
        Qualtrics.SurveyEngine.setEmbeddedData("used", used);
        
        console.log(good);
        console.log(used);
    };
        
});

My survey flow:
qualtrics2.PNG
My question block:
qualtrics1.PNG
What was actually printed out during Preview, and Console:
qualtrics3.PNGAfter clicking Next:
image.pngWhat is happening why isn't my code working?
Thanks!

1 reply

grahulp5
QPN Level 3 ●●●
Forum|alt.badge.img+18
  • QPN Level 3 ●●●
  • Nov 9, 2021 6:58 PM UTC

Sometimes it takes time to update embedded via js. I'd suggest to use survey flow instead of js and then pipe the text randomly.
Alternatively you can use a multipunch question to randomly assign 1 of the values to an embedded and then just set it to an embedded and use everything in different question.