Array not indexed at zero? | XM Community
Question

Array not indexed at zero?

  • 24 October 2020
  • 1 reply
  • 8 views

Badge

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();

});


1 reply

Badge

Problem solved(ish)!
So, you know how Qualtrics tracks when you delete response options? My survey had "default" recoded values of 4-15 on the critical question. Manually setting the recodes to 1-12 or clearing recodes did not fix the problem, even with all test responses deleted. When Qualtrics support COPIED the survey the problem was fixed. Apparently a copy of a survey does not carry over the "history" of deleted response options. Oddly, this only appears to be true if the survey is copied by Qualtrics support. I tried it myself and it failed.
The apparent failure of the search function was due to a combination of the other problem and the search function putting capitalized terms first.

Leave a Reply