Using JavaScript to Calculate Lowest 3 Category Scores | XM Community
Skip to main content
Question

Using JavaScript to Calculate Lowest 3 Category Scores

  • February 23, 2025
  • 20 replies
  • 142 views

Forum|alt.badge.img+2

Hi all,


Looking for someone with JavaScript knowledge to help me with my custom script (I have no experience, I just learn by figuring the code out visually in this case). 

Essentially the survey has 6 categories. The user will answer a quiz with questions under those categories and score accordingly. We want the end of survey message to calculate the user’s lowest 3 category scores so we can send a customised message to recommend courses in those categories for the user to take. 

My issue is that the calculated lowest 3 scores do not appear and I’m not sure why. Appreciate any assistance on this :)

Sample message:

Thank you for completing the survey! Based on your responses, we've identified the three categories where you scored the lowest. To help you improve in these areas, we recommend the following courses:

  1. [Category 1]: [Course Recommendation]
  2. [Category 2]: [Course Recommendation]
  3. [Category 3]: [Course Recommendation]

Here is the code I have currently, as a question at the end with JavaScript: 

 

Qualtrics.SurveyEngine.addOnload(function()

{var scores = {

"The Lesson": "${e://Field/TheLessonScore}",

"Assessment And Feedback": "${e://Field/AssessmentAndFeedbackScore}",

"The Study": "${e://Field/TheStudyScore}",

"Differentiation": "${e://Field/DifferentiationScore}",

"The Tutorial": "${e://Field/TheTutorialScore}",

"Applying The Student Behaviour Policy": "${e://Field/ApplyingTheStudentBehaviourPolicyScore}"   

};

 

// Sort scores to find the lowest

var sortedScores = Object.keys(scores).sort(function(a, b) {

return scores[a] - scores[b]; 

});

 

// Set the lowest category names in embedded data fields

Qualtrics.SurveyEngine.setEmbeddedData("LowestCategory1", sortedScores[0].name);

Qualtrics.SurveyEngine.setEmbeddedData("LowestCategory2", sortedScores[1].name);

Qualtrics.SurveyEngine.setEmbeddedData("LowestCategory3", sortedScores[2].name);

});

Below is the customised End of Survey code and preview message:

 


Thank you!

20 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • February 24, 2025

@LisaNguyen,

You should set up your items as an array of objects, then sort:

Qualtrics.SurveyEngine.addOnload(function() {
var scores = [
{name:"The Lesson", score:"${e://Field/TheLessonScore}"},
{name:"Assessment And Feedback", score:"${e://Field/AssessmentAndFeedbackScore}"},
{name:"The Study", score:"${e://Field/TheStudyScore}"},
{name:"Differentiation", score:"${e://Field/DifferentiationScore}"},
{name:"The Tutorial", score:"${e://Field/TheTutorialScore}"},
{name:"Applying The Student Behaviour Policy", score:"${e://Field/ApplyingTheStudentBehaviourPolicyScore}"
];

// Sort scores to find the lowest
scores.sort(function(a, b) { a.score - b.score; });

jQuery.each(scores, function(i) {
Qualtrics.SurveyEngine.setEmbeddedData("LowestCategory"+(i+1),this.name);
});
});

 


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • February 24, 2025

Thanks ​@TomG

 

I’ve added the JavaScript you advised and I’m not sure how to have it display the lowest 3 category scores in the End of Survey message. Appreciate your help on this as well.

Thank you for completing the survey! Based on your responses, we've identified the three categories where you scored the lowest. To help you improve in these areas, we recommend the following courses:

  1. [Lowest Category 1]: [Course Recommendation]
  2. [Lowest Category 2]: [Course Recommendation]
  3. [Lowest Category 3]: [Course Recommendation]

 

Great job! According to your scores on the Phase 3 pre-survey, you may benefit from the Lessons in the following categories:

  1. ${e://Field/LowestCategory}
  2. ${e://Field/LowestCategory}
  3. ${e://Field/LowestCategory}

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • February 25, 2025

Add 1, 2, 3 to the end of the embedded data fields (e.g., ${e://Field/LowestCategory1})


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • February 25, 2025

Thanks Tom, I tried to update it but I see nothing.

Great job! According to your scores on the Phase 3 pre-survey, you may benefit from the Lessons in the following categories:

  1. ${e://Field/LowestCategory1}
  2. ${e://Field/LowestCategory2}
  3. ${e://Field/LowestCategory3}

Test 2 - ${e://Field/LowestCategory1} ${e://Field/LowestCategory2} ${e://Field/LowestCategory3}

 

 

Preview below: 

 

And this is my survey flow for reference:

 

 


vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • February 25, 2025

You seem to be filling your embedded data score fields after your js script. Try moving the ED element before your JavaScript [end of survey] block.


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • February 25, 2025

Hi ​@vgayraud,

Thanks for your input :).

 

I tried moving the ED element before the Javascript but nothing still shows.

 

Preview below:

 


vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • February 25, 2025

Unless they’re higher in your survey flow, you don’t seem to have ED fields to store your results (LowestCategory1, LowestCategory2 and LowestCategory3). You seem to be piping in undefined ED fields.


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • February 25, 2025

You're right on that (I don’t have it on my survey flow). I’m not sure what to put down as the ED field for these.

 

i.e. ED “LowestCategory1” = *[not sure what to put here for value]*


vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • February 25, 2025

You don’t need to put any value. Leave them empty and they’ll be filled by your script. They just need to be declared.

 


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • February 25, 2025

Thanks, I tried this but still same result. 

 


vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • February 25, 2025

I’m not too sure then.. Are you using the “new survey taking experience” layout or one of the older layouts?

 


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • February 25, 2025

Appreciate the screenshots!

I got the 1st screenshot as “on” fort the New survey taking experience. I don’t see the “layout” tab on your 2nd screenshot so I will look into that with Qualtrics. 


vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • February 25, 2025

Ok, then you can’t use the old .setEmbeddedData.

Your script should be like this :

Qualtrics.SurveyEngine.addOnload(function() {
var scores = [
{name:"The Lesson", score:"${e://Field/TheLessonScore}"},
{name:"Assessment And Feedback", score:"${e://Field/AssessmentAndFeedbackScore}"},
{name:"The Study", score:"${e://Field/TheStudyScore}"},
{name:"Differentiation", score:"${e://Field/DifferentiationScore}"},
{name:"The Tutorial", score:"${e://Field/TheTutorialScore}"},
{name:"Applying The Student Behaviour Policy", score:"${e://Field/ApplyingTheStudentBehaviourPolicyScore}"
];

// Sort scores to find the lowest
scores.sort(function(a, b) { a.score - b.score; });

jQuery.each(scores, function(i) {
Qualtrics.SurveyEngine.setJSEmbeddedData("LowestCategory"+(i+1),this.name);
});
});

Your EDs like this :

And your end of survey piped in text should be :

${e://Field/__js_LowestCategory1}

 


vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • February 25, 2025

Also, ​@TomG ‘s script is using jQuery, which isn’t included in the new layout style. You’ll want to add this to your header, in source mode :

<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><script>

 


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • February 25, 2025

Still doesn’t work unfortunately :(
 

Javascript on question at the end:

Survey workflow:


Preview end of survey message:

 


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • February 25, 2025

@vgayraud 

I tried the header script too. Same result as previous comment unfortunately. 

 


vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • February 25, 2025

 

Qualtrics.SurveyEngine.addOnload(function() {
var scores = [
{name:"The Lesson", score: parseFloat("${e://Field/TheLessonScore}")},
{name:"Assessment And Feedback", score: parseFloat("${e://Field/AssessmentAndFeedbackScore}")},
{name:"The Study", score: parseFloat("${e://Field/TheStudyScore}")},
{name:"Differentiation", score: parseFloat("${e://Field/DifferentiationScore}")},
{name:"The Tutorial", score: parseFloat("${e://Field/TheTutorialScore}")},
{name:"Applying The Student Behaviour Policy", score: parseFloat("${e://Field/ApplyingTheStudentBehaviourPolicyScore}")}
];

scores.sort(function(a, b) {
return a.score - b.score;
});

scores.forEach(function(item, index) {
Qualtrics.SurveyEngine.setJSEmbeddedData("LowestCategory" + (index + 1), item.name);
});

});

 


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • February 25, 2025

How do I open/use the attachment?


vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • February 25, 2025

It’s a .qsf file (qualtrics survey definition file), but weirdly we can’t attach .qsf in here. You can remove the .txt extension and create a new questionnaire within Qualtrics with it.

It’s just a demo though with generic scores and category names, the updated code I gave you should work with your setup.

Good luck! :)


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • February 25, 2025

Thanks so much. I'm struggling to import the qsf because it comes up as text. I don't have the option to remove the txt extension as well it seems.