Hello all,
I'm new to javascript and have been adapting Kurt Munz's method of carrying forward rank order questions:
Updated: Rank-Order Carry Forward in Qualtrics with Javascript - Kurt MunzIt actually works fine. The rank order in question 1 is the item order in the constant sum question 2. The problem is that I then want to feed the constant sum data into a new array, sort the array based on the item descriptor, and then save those values as embedded variables. It is my way of putting the variables back into some kind of order for later data analysis. Otherwise the constant sum data is organized by the order the subject ranked things, rather than some constant order I can feed into a stat program.
Two things aren't working:
1.) it isn't sorting based on the descriptor.
2.) For some reason the new array indexes at [4] rather than [0]. So '${q://QID5/ChoiceNumericEntryValue/1}' is recorded in choice[4][0] rather than choice [0][0] (and so on through the list). Any thoughts on this? The former issue I can probably figure out by googling more on javascript. But I'm deeply confused on the latter. All embedded variables are declared in the survey flow.
Qualtrics.SurveyEngine.addOnload(function()
{ // hide next button
$('NextButton') && $('NextButton').hide();
// store score in first column, description in second column of array "choice"
var choice = [
['${q://QID5/ChoiceNumericEntryValue/1}','${q://QID5/ChoiceDescription/1}'],
['${q://QID5/ChoiceNumericEntryValue/2}','${q://QID5/ChoiceDescription/2}'],
['${q://QID5/ChoiceNumericEntryValue/3}','${q://QID5/ChoiceDescription/3}'],
['${q://QID5/ChoiceNumericEntryValue/4}','${q://QID5/ChoiceDescription/4}'], ['${q://QID5/ChoiceNumericEntryValue/5}','${q://QID5/ChoiceDescription/5}'],
['${q://QID5/ChoiceNumericEntryValue/6}','${q://QID5/ChoiceDescription/6}'],
['${q://QID5/ChoiceNumericEntryValue/7}','${q://QID5/ChoiceDescription/7}'],
['${q://QID5/ChoiceNumericEntryValue/8}','${q://QID5/ChoiceDescription/8}'],
['${q://QID5/ChoiceNumericEntryValue/9}','${q://QID5/ChoiceDescription/9}'],
['${q://QID5/ChoiceNumericEntryValue/10}','${q://QID5/ChoiceDescription/10}'],
['${q://QID5/ChoiceNumericEntryValue/11}','${q://QID5/ChoiceDescription/11}'],
['${q://QID5/ChoiceNumericEntryValue/12}','${q://QID5/ChoiceDescription/12}']
];
// sort choice BY DESCRIPTION
choice.sort(sortFunction);
function sortFunction(a, b) {
if (a[1] === b[1]) {
return 0;
}
else {
return (a[1] < b[1]) ? -1 : 1;
}
}
Qualtrics.SurveyEngine.setEmbeddedData('firstEND',choice[0][0]);
Qualtrics.SurveyEngine.setEmbeddedData('secondEND',choice[1][0]);
Qualtrics.SurveyEngine.setEmbeddedData('thirdEND',choice[2][0]);
Qualtrics.SurveyEngine.setEmbeddedData('fourthEND',choice[3][0]);
Qualtrics.SurveyEngine.setEmbeddedData('fifthEND',choice[4][0]);
Qualtrics.SurveyEngine.setEmbeddedData('sixthEND',choice[5][0]);
Qualtrics.SurveyEngine.setEmbeddedData('seventhEND',choice[6][0]);
Qualtrics.SurveyEngine.setEmbeddedData('eighthEND',choice[7][0]);
Qualtrics.SurveyEngine.setEmbeddedData('ninthEND',choice[8][0]);
Qualtrics.SurveyEngine.setEmbeddedData('tenthEND',choice[9][0]);
Qualtrics.SurveyEngine.setEmbeddedData('eleventhEND',choice[10][0]);
Qualtrics.SurveyEngine.setEmbeddedData('twelfthEND',choice[11][0]);
// advance to next screen
this.clickNextButton();
});
Question
Array not indexed at zero?
Leave a Reply
Rich Text Editor, editor1
Editor toolbars
Press ALT 0 for help
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.