Create weights in JavaScript based on ranking question | XM Community
Skip to main content
I need to create weights in-survey for rank-order in drag/drop ranking questions. Example, rate these car brands on safety: BMW, Volvo, Ford, Kia,BToyota The #1 ranked car gets a weight of 1, 2nd ranked a weight of .75, 3rd ranked a weight of .5, 4th a weight of .25, 5th a weight of 0. These weights will be stored in embedded variables like BMW_Safety_Weight, Volvo_Safety Weight, etc. How can I do this using javascript? Thanks!



Extra credit: We have an embedded variable called BrandCount (between 2 and 10 brands) The above example is a 5 point scale, for 5 brands. If 4 brands were to be ranked, the weights would be 1, .67, .33, 0. Ideally we'd like to apply the weighting scale that corresponds to the BrandCount, but I can figure that out later. As always, your help is greatly appreciated!
You first have to store first 5 ranks in embedded data and than using branch logic you can assign weights.



Or you can use scoring too.
Hi @bansalpeeyush29 We are trying to do it in Java because there isn't the explosion of # of branches required with branch logic. For instance, there would be a lot of branches just to determine which Rank BMW had, then if BMWrank = 1, BMWweight =1, if BMWrank = 2, BMWweight = .75, if BMWrank = 3, BMWweight = .5, if BMWrank=4, BMWweight=.25, if BMWrank=5, BMWweight=0.



THat's 5 IF statements just for 1 brand for one criteria. If we are ranking 5 brands on 5 criteria that would be to be 125 branches. I read JavaScript has a sort function which would make it easy to sort the 5 brands by rank, and then just substitute weight for rank.



If I even knew the "internal variables" Qualtrics uses to store the rank information for each choice, I would probably figure out how to hack some javascript I find on line.
Hello @Tom_R ,



Assuming you are using "Rank order" -> "Drag and Drop" question type.



Step 1: Create five embedded data(BMW_Safety_Weight, Volvo_Safety_Weight, Ford_Safety_Weight, Kia_Safety_Weight, BToyota_Safety_Weight) in the survey flow before the rank order question. Now, please note the vehicle name in the embedded data must be exactly same with that provided in the question's option.(This is done to make js code smaller)



Step 2: Paste the following code in the `js(onReady)` of the rank order of the question.



jQuery(document).on('DOMSubtreeModified', function(){

var aa=[];

jQuery("ul.ui-sortable li.ui-sortable-handle > span.label").each(function(){

aa.push(jQuery(this).text().trim());



Qualtrics.SurveyEngine.setEmbeddedData( aa[0]+'_Safety_Weight', '1' );

Qualtrics.SurveyEngine.setEmbeddedData( aa[1]+'_Safety_Weight', '0.75' );

Qualtrics.SurveyEngine.setEmbeddedData( aa[2]+'_Safety_Weight', '0.5' );

Qualtrics.SurveyEngine.setEmbeddedData( aa[3]+'_Safety_Weight', '0.25' );

Qualtrics.SurveyEngine.setEmbeddedData( aa[4]+'_Safety_Weight', '0' );

});

});



Step 3: Now use those five embedded data(BMW_Safety_Weight, Volvo_Safety_Weight, Ford_Safety_Weight, Kia_Safety_Weight, BToyota_Safety_Weight) wherever required

Leave a Reply