I have four different questions in four separate blocks. I want each respondent to get A. B, C, D, and 1, 2, 3, 4 only once.
For example, I don't want respondents to view images of A1, B1, C1, D1 or A1, A2, A3, A4. I want my respondents to get images A2, B1, C4, D3, or A4, B2, C1, D3.
Currently, my JS code seems to just grab images from the array. It's a bit difficult to test, and I'm not sure if I did the slice() correctly.
Also, my firstImage seems to not show up unless I refresh the page. Do you know why this is happening?
Here's my JS code below:
Qualtrics.SurveyEngine.addOnload(function(){
function shuffle(array){
var counter = array.length,
temp, index;
while (counter > 0){
index = Math.floor(Math.random() * counter);
counter = counter-1;
temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
var A1 = {'A1' : "https://imgA1"};
var A2 = {'A2' : "https://imgA2"};
var A3 = {'A3' : "https://imgA3"};
var A4 = {'A4': "https://imgA4"};
var B1 = {'B1' : "https://imgB1"};
var B2 = {'B2' : "https://imgB2"};
var B3 = {'B3' : "https://imgB3"};
var B4 = {'B4': "https://imgB4"};
var C1 = {'C1' : "https://imgC1"};
var C2 = {'C2' : "https://imgC2"};
var C3 = {'C3' : "https://imgC3"};
var C4 = {'C4': "https://imgC4"};
var D1 = {'D1' : "https://imgD1"};
var D2 = {'D2' : "https://imgD2"};
var D3 = {'D3' : "https://imgD3"};
var D4 = {'D4': "https://imgD4"};
var myArray = [A1, A2, A3, A4, B1, B2, B3, B4, C1, C2, C3, C4, D1, D2, D3, D4];
shuffle(myArray);
Qualtrics.SurveyEngine.setEmbeddedData("firstImage",Object.values(myArray[0])[0]);
Qualtrics.SurveyEngine.setEmbeddedData("firstImageName",Object.keys(myArray[0])[0]);
var pick = Object.keys(myArray[0])[0].slice(1)
var myArray = myArray.filter(val => !Object.keys(val)[0].endsWith(pick))
shuffle(myArray)
Qualtrics.SurveyEngine.setEmbeddedData("secondImage",Object.values(myArray[0])[0]);
Qualtrics.SurveyEngine.setEmbeddedData("secondImageName",Object.keys(myArray[0])[0]);
var pick = Object.keys(myArray[0])[0].slice(1)
var myArray = myArray.filter(val => !Object.keys(val)[0].endsWith(pick))
shuffle(myArray)
Qualtrics.SurveyEngine.setEmbeddedData("thirdImage",Object.values(myArray[0])[0]);
Qualtrics.SurveyEngine.setEmbeddedData("thirdImageName",Object.keys(myArray[0])[0]);
var pick = Object.keys(myArray[0])[0].slice(1)
var myArray = myArray.filter(val => !Object.keys(val)[0].endsWith(pick))
shuffle(myArray)
Qualtrics.SurveyEngine.setEmbeddedData("fourthImage",Object.values(myArray[0])[0]);
Qualtrics.SurveyEngine.setEmbeddedData("fourthImageName",Object.keys(myArray[0])[0]);
});
How do I filter out images for randomization without replacement?
Best answer by JeremyK
For why your first image isn't showing up until after a page refresh, I can answer that one now. After looking at your first question, you're trying to use ${e://Field/firstImage} in the question source, which works great, it's an order of operations problem; you're trying to use firstImage before it's been set.
When your survey first loads, the firstImage embedded data field hasn't been set yet. The page loads, the question is displayed, then your JS runs and sets the firstImage field. When you refresh the page, now the firstImage has been set and you can use it, no problem.
The easiest fix for this is a lead-in question (a welcome question, if you will) in a separate block (or a page break; just needs to be on a previous page). Your JS will have to run on that first page to set the embedded data fields up. Then you can use them on subsequent pages.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
