Hi - I am designing a survey with a series of rank order questions that I need to work as follows:
Question 1: Rank these items where 1 = most preferred
1) Cat
2) Dog
Question 2: Your previous rankings have been provided along with an additional item. Please rank these items again where 1 = most preferred.
1) Cat
2) Dog
3) Fish
This will continue until the participant has ranked 20 items. In order to do this, I adapted Kurt Munz's code (http://kurtmunz.com/rank-order-carry-forward-in-qualtrics-with-javascript/) for the first 9 items which works well. Given that his code utilizes an alphabetical sort, I need an alternate method to sort once I reach 10+ items. I adapted this code (https://stackoverflow.com/questions/51736930/carry-forward-new-rank-order-from-pick-group-rank-selected-choices-to-slider/51746460) to parse Qualtric's choice numeric entry value into a numerical rank and sort using that. Unfortunately, something about this slight change has caused the text in the embedded data fields 'first' and 'second' which are piped into the next question to no longer appear. I've included my code below (I used the alphabetical sort to test since I know it works) along with the output from console.log(choice).
{
var choice = [
{'rank': parseInt('${q://QID206/ChoiceNumericEntryValue/2}'),'label': '${q://QID206/ChoiceDescription/2}'},
{'rank': parseInt('${q://QID206/ChoiceNumericEntryValue/4}'),'label': '${q://QID206/ChoiceDescription/4}'}
];
console.log(choice)
// sort choice
choice.sort()
// save description of first and second choice as embedded data
Qualtrics.SurveyEngine.setEmbeddedData('first',choicei0][1]);
Qualtrics.SurveyEngine.setEmbeddedData('second',choicei1][1]);
});
Given that my array is displaying correctly with the correct ranks and labels (and the display looks the same as the console.log using the code that works), I can't figure out why my embedded data labels no longer appear. I've reviewed this forum and Stack overflow and am totally out of ideas so I'd very much appreciate suggestions!
You are mixing up arrays and objects. Take another look at the stackoverflow example (I posted that). You need a sort function and the first label would be choice[0]['label'].
Thank you SO much. I had the sort function written into my code but hadn't included it in the example. The choice[0]['label'] was exactly what I needed to get it to run.
Updated code below for anyone who wants to use this in the future:
{
var choice = [
{'rank': parseInt('${q://QID206/ChoiceNumericEntryValue/2}'),'label': '${q://QID206/ChoiceDescription/2}'},
{'rank': parseInt('${q://QID206/ChoiceNumericEntryValue/4}'),'label': '${q://QID206/ChoiceDescription/4}'}
];
console.log(choice)
// sort choice by rank
choice.sort(function(a,b) { return a.rank - b.rank; });
// save description of first and second choice as embedded data
Qualtrics.SurveyEngine.setEmbeddedData('first',choiceh0]c'label']);
Qualtrics.SurveyEngine.setEmbeddedData('second',choiceh1]c'label']);
});
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.