Javascript - assign names randomly from 2 lists | XM Community
Question

Javascript - assign names randomly from 2 lists

  • 26 April 2020
  • 2 replies
  • 9 views

Hi,
I want to show 40 pictures with names to the participants. The order of the pictures and the pairs (picture + name) need to be randomized. At the moment I have each picture as a single question randomized with question randomization. I put the names in Javascript (I`m totally new to Javascript) and they are drawn randomly. But the names can not be drawn completely random as I have two lists of names and want certain pictures to be paired with any (random) name from list 1 but not list 2 and the other way around. In addition to that I need each name to be assigned to one picture just once. How can I achieve this? What code do I need to use?
Thank you so much already!

So far I`ve used this code:
Qualtrics.SurveyEngine.addOnReady(function() {
var words = ["Thomas", "James", "Tom", "Daniel"]; //Your words
var i = Math.floor(Math.random() * words.length); //randomly select an index
var my_word = words[i]; //select a word
Qualtrics.SurveyEngine.setEmbeddedData( 'word', my_word); //Send the word to the Qualtrics output, otherwise you would not know which participant get which word
document.getElementById("random").innerHTML = my_word; //show the word to respondents
});


2 replies

Userlevel 7
Badge +22

Use loop and merge based on two column. First column will contain image url and second will contain the list type from where the name is to be fetched( so it will contain only two value eg - list1/ list2).
Create 40 embedded data one for each picture/question, i.e. it will contain the word that was displayed with respective picture for analysis.
Create 2 more ED i.e. UsedWord1 (for list1), UsedWord2 (for list2), these embedded data will get added with value i.e. displayed.
Now, In the JS check the list type using Filed2 of loop and merge and assign random name from respective list. Before assigning compare list1 with UsedWord1 , list2 with UsedWord12 and remove duplicates. Assign selected word to respective ED ( from 40 ED) based on loop number.


Leave a Reply