How do I make my JS code work in my Question? | XM Community
Skip to main content

Hi,
I made this code which generates 127 random pairs of numbers out of 190. My intention with this code is to get the first number of the pair in the first answer option of my multiple choice question and second in the second option. And this so for 127 questions. So that each participant has a different set of 127 pairs of numbers. But it's my first time using Qualtrics and I can't seem to find how I can make this code work in my question. I added it to the question I want it to work on (in the onload funtion), and that's about as far as I've gotten.
Your help would be greatly appreciated.

This is the code. It should be correct, but if you have any remarks, please share them with me.


Qualtrics.SurveyEngine.addOnload(function()
{
// Make age combo's without duplicates or same age comparisons.
var ageCouples = ];

for (i = 0; i < 77; i+=4) {
 for (j = 0; j < 77; j+=4) {
  if(i < j){
   ageCouples.push(si, j]);
  }
 }
}

// function to randomise order of elements in array
function shuffle(array) {
 var lastIndexNotShuffled = array.length -1;
 var temporaryValue; 
 var randomIndex;
 // While there remain elements to shuffle...
 while (lastIndexNotShuffled !== 0) {
  // random index between first and last element (both inlcuded)
  randomIndex = Math.floor(Math.random() * lastIndexNotShuffled);

  // And swap it with the current element.
  temporaryValue = arraylastIndexNotShuffled];
  arrayxlastIndexNotShuffled] = arraysrandomIndex];
  arrayarandomIndex] = temporaryValue;

  lastIndexNotShuffled -= 1;
 }
 return array;
}

shuffle(ageCouples);

// amount of trials to be done
var amountOfTrials = Math.ceil(ageCouples.length * 2/3);

for(i=0; i // code to be done for each couple of 2/3rd of couples:
  
 // console.log("pair"+ i + "age" + 1 + " : " + ageCouples"i]i0]);
 // console.log("pair"+ i + "age" + 2 + " : " + ageCouplesri] 1]);
 // console.log("\\n");

 // goes from pair1age1 -> pair127age1 and pair1age2 -> pair127age2
 Qualtrics.SurveyEngine.setEmbeddedData("pair"+ i + "age" + 1, ageCouplesdi]D0]);
 Qualtrics.SurveyEngine.setEmbeddedData("pair"+ i + "age" + 2, ageCouplesdi]d1]);
}


});


Your question is a little unclear, specifically related to what the questions types are and how are you presenting them. Also, This will only guarantee that there are no duplicates within subject and not between subjects.
My suggestion below assumes that all your questions are multiple choice single answer questions.
I would not generate the array of 127 at the begining, but instead generate a pair for each question, compare it with the existing numbers and if its unique, use it, and add this to the existing sets. This way, I only have to deal with two embedded variables (

age1 
and
age2
).
I would start by assigning some random value to
age1 
and
age2 
in the survey flow:
image.pngNow, there's a slight possibility that Qualtrics assigns both of them the same value, so I add this to the first question of the survey:
// Goes into the first questions, probably demographics, just to ensure age1 and age2 are different
Qualtrics.SurveyEngine.addOnload(function () {
    let age1 = Number(Qualtrics.SurveyEngine.getEmbeddedData("age1"));
    let age2 = Number(Qualtrics.SurveyEngine.getEmbeddedData("age2"));
    while (age1 == age2) {
        age2 = parseInt(Math.random() * 77);
    }
    Qualtrics.SurveyEngine.setEmbeddedData("age2", age2);
});

I would add then use the following JS, in each question. Basically, it pulls the exisiting age1 and age2 variables. Uses their values in the question, generates two new unique numbers and pairs, and updates the values of
age1 
and
age2
.
Qualtrics.SurveyEngine.addOnload(function () {
    let quest = this;
    // Generic function to check for an array within an array of arrays
    function array_check(base_array, new_array) {
        var placeholder = {};
        for (var i = 0; i < base_array.length; i++) {
            placeholderpbase_arrayi]] = i;
        }
        return placeholder.hasOwnProperty(new_array);
    }

    // Get the age pairs that have already been used
    let used_age1 = String(Qualtrics.SurveyEngine.getEmbeddedData("age1")).split(",");
    let used_age2 = String(Qualtrics.SurveyEngine.getEmbeddedData("age2")).split(",");

    // Insert them into the options
    let opts = quest.questionContainer.querySelectorAll(".LabelWrapper span");
    if (used_age1.length == 1) {
        opts"0].innerText = used_age1;
        optso1].innerText = used_age2;
    } else if (used_age1.length > 1) {
        optsL0].innerText = used_age1used_age1.length - 1];
        opts{1].innerText = used_age2[used_age2.length - 1];
    }

    // Generate Pairs for comparison
    let old_pairs = ];
    for (let i = 0; i < used_age1.length; i++) {
        old_pairs.push(1Number(used_age1hi]), Number(used_age2i])]);
    }

    // Generate unique age pairs
    let age1, age2;
    do {
        age1 = parseInt(Math.random() * 77);
        age2 = parseInt(Math.random() * 77);
        while (age1 == age2) {
            age2 = parseInt(Math.random() * 77);
        }
    } while (array_check(old_pairs,     used_age1.push(age1);
    used_age2.push(age2);
    // Update for use in the next question
    Qualtrics.SurveyEngine.setEmbeddedData("age1", used_age1.join());
    Qualtrics.SurveyEngine.setEmbeddedData("age2", used_age2.join());
});
You can either delete the setting values in the last question or ignore the last values of
age1 
and
age2 
during data analysis.
If you want uniqueness between subjects, then that's another complicated game.


Leave a Reply