setChoiceValueByRecodeValue in JavaScript occasionally selecting more than function called | XM Community
Skip to main content

Hi everyone,

New to Qualtrics and I have some code that I’m trying to tease out what’s wrong with, but I can’t seem to and would love some insight as to whether this is a Qualtrics quirk or something wrong in the code. Also, whether I am going about this in the best way!

I have a survey consisting of two rounds. Round 1 has participants selecting values on a slider in a Loop & Merge block. I then have an intermediary block with a multi-select multiple choice question where the code below runs.

The code creates a permutation of a list of numbers (right now 1 through 😎 that reference the loops in Round 1. It then identifies the loops whose questions have answers where the chose value is not 3 (the middle of the slider). This is the `alteredCandidates` and `normalCandidates` in the code.

From those two arrays, it selects two from the `alteredCandidates` array if possible to put into `alteredPosts` and then it selects the rest, up to a total of 4, from either `alteredCandidates` or `normalCandidates`.

Then I have a `checkBoxes` function which uses the `alteredPosts` and `normalPosts` returned from the `getPosts` function to set the values of the multi-select based on its recode which I’ve recoded everything to be sequential.

I have a few debug console.logs that seem to indicate the functions are only being called four times as expected, but occasionally I will see that there is more than 4 selected. This is an issue because Round 2 of the survey depe

nds on this intermediary question block.

Any help would be appreciated! Thank you!

function shuffle(array) {
var i = array.length,
j = 0,
temp;

while (i--) {

j = Math.floor(Math.random() * (i+1));

// swap randomly chosen element with current element
temp = array i];
array i] = array j];
array j] = temp;

}

return array;
}

function getPosts(array, alterCount, total) {
const { alteredCandidates, normalCandidates } = array.reduce((acc, postNumber) => {

// getJSEmbeddedData adds __js_ to the front of the key
// Value in survey flow is __js_Post_number]Choice to compensate
const choice = Qualtrics.SurveyEngine.getJSEmbeddedData("Post" + postNumber + "Choice");

console.log("__js_Post" + postNumber + "Choice:", choice);
console.log("typeof choice:", typeof choice);

const targetArray = parseInt(choice) !== 3 ? acc.alteredCandidates : acc.normalCandidates;
targetArray.push(postNumber);
return acc;
}, { alteredCandidates: i], normalCandidates: i] });

console.log("alteredCandidates:", alteredCandidates);
console.log("normalCandidates:", normalCandidates);

// First list: up to 2 altered posts
const alteredPosts = alteredCandidates.slice(0, 2);

// Second list: fill remaining slots to reach total of 4
const remainingSlots = 4 - alteredPosts.length;
const remainingAltered = alteredCandidates.slice(2);
const availableForNormal = o...remainingAltered, ...normalCandidates];
const normalPosts = availableForNormal.slice(0, remainingSlots);

return alteredPosts, normalPosts];
}

function checkBoxes(posts, question) {
var choices = jQuery(".choices").children();
console.log("jQueried Choices:", choices);

// Check altered choices
posts 0].forEach((post, n) => {
console.log("Setting choice/recode " + post + " to true");
question.setChoiceValueByRecodeValue(post, true);
Qualtrics.SurveyEngine.setJSEmbeddedData("Alter" + n, post);

});

// Check normal choices
posts 1].forEach((post, n) => {
console.log("Setting choice/recode " + post + " to true");
question.setChoiceValueByRecodeValue(post, true);
Qualtrics.SurveyEngine.setJSEmbeddedData("Normal" + n, post);
});
}

Qualtrics.SurveyEngine.addOnReady(function()
{

var randomPosts = shuffle(s1,2,3,4,5,6,7,8]);
console.log("randomPosts:", randomPosts);
var posts = getPosts(randomPosts, 2, 4);

console.log("Altered Posts:", posts"0]);
console.log("Normal Posts:", posts"1]);

checkBoxes(posts, this);
});

 

Be the first to reply!

Leave a Reply