Javascript: How to Subtract Up to 3 Points on Item for Minimum Value of "0"? | XM Community
Skip to main content

Good Morning, You Lovely People!
 

I have a Lifecycle survey where I want to have an overall “score” (raw number - not a 😵 calculated based on the items selected in the survey.

  • The maximum/”perfect” score is 30
  • There are 8 questions - each with different values (3 answer options or 5 answer options)
  • 7 of the questions are straightforward (A = 5, B = 4, etc.)
  • However: On one of the questions - there are 17 possible answers
    • I want to have the system *subtract* a point for each item selected - subtracting up to 3 points for a minimum value of “0”
      • So: Max Score for the question is 3
      • Minimum score is 0
      • If 1 item is selected: Subtract 1 point
      • If 3 items selected: Subtract 3 points
      • If 4 or more items selected: Subtract 3 points

I added a custom javascript (mentioned below) and included it as an embedded data field in the flow before the questions.

Unfortunately - it’s not working the way I want it to.

So now - I’m wondering if such a thing is possible *or* if I’d need to think of a different structure to the item.

Thank y’all for any help you can provide!

 

Script

Qualtrics.SurveyEngine.addOnload(function() {
  this.questionclick = function(event, element) {
    // Count how many checkboxes are checked
    var selectedCount = this.getSelectedChoices().length;
 
    // Cap score between 0 and 3
    var score = Math.max(0, 3 - selectedCount);
 
    // Store in embedded data
    Qualtrics.SurveyEngine.setEmbeddedData("Q20_Score", score);
  };
});

Try:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
// Count how many checkboxes are checked
var selectedCount = this.getSelectedChoices().length;

// Cap score between 0 and max
var max = 3;
if(selectedCount>max) selectedCount = max;
var score = max - selectedCount;

// Store in embedded data
Qualtrics.SurveyEngine.setEmbeddedData("Q20_Score", score);
});

 


Hey Tom!

Thank you for the reply.

So - for the attachments…

  • Image 1 is the survey item
  • Image 2 is the coder you provided (that i entered into the item)
  • Image 3 shows the dashboard & - unfortunately - it’s not working
  • The first column (Challenges (Java)) - if the calculation the works as intended - there should be a “0” in that column (because - for either of those tests - I chose more than 3 items)

Let me know if you need anything else from me on this (or if I missed a step)

I appreciate your knowledge!


Now that I see your screenshot, the issue is Simple layout (aka New Experience). You have to use:

Qualtrics.SurveyEngine.setJSEmbeddedData("Q20_Score", score);

Before the question block, you have to define the embedded data field ‘__js_Q20_Score’ in the survey flow and that will be the name of the field in your response data.


Hey Tom!

Thank you for your message.

Unfortunately - I’m confused (not bc of you - I’m new to the whole javascript thing).

 

So - to confirm - is the below correct script?

 

Qualtrics.SurveyEngine.addOnPageSubmit(function() { // Count how many checkboxes are checked var selectedCount = this.getSelectedChoices().length; // Cap score between 0 and max var max = 3; if(selectedCount>max) selectedCount = max; var score = max - selectedCount; // Store in embedded data Qualtrics.SurveyEngine.setEmbeddedData("Q20_Score", score); });

 

And then - in the flow - is the attached correct?

 

 

 


Hey Tom!

Thank you for your message.

Unfortunately - I’m confused (not bc of you - I’m new to the whole javascript thing).

 

So - to confirm - is the below correct script?

 

Qualtrics.SurveyEngine.addOnPageSubmit(function() { // Count how many checkboxes are checked var selectedCount = this.getSelectedChoices().length; // Cap score between 0 and max var max = 3; if(selectedCount>max) selectedCount = max; var score = max - selectedCount; // Store in embedded data Qualtrics.SurveyEngine.setEmbeddedData("Q20_Score", score); });

 

And then - in the flow - is the attached correct?

 

 

 

No, it isn’t. Because you are using ‘New Experience’ for your survey, you have to define the Embedded Data field in the survey flow as __js_Q20_Score instead of Q20_Score.  Then the JavaScript is:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
// Count how many checkboxes are checked
var selectedCount = this.getSelectedChoices().length;

// Cap score between 0 and max
var max = 3;
if(selectedCount>max) selectedCount = max;
var score = max - selectedCount;

// Store in embedded data
Qualtrics.SurveyEngine.setJSEmbeddedData("Q20_Score", score);
});

(note the function name change in the last line)


Hey Tom!

Thank you for all of your work on this.

Unfortunately - it’s still not working as intended.

I've provided screenshots of the exact setup - so feel free to let me know if it’s just not possible (I’d hate for you to keep spending time on this when you have an actual job to do 😆)

Images attached…

  1. The updated javascript for Q20
  2. How the question looks in Survey Builder
  3. The survey flow (red arrow = updated embedded data flow)
  4. How the question is mapped in the dashboard data (yellow arrows)
  5. How it looks in the dashboard (red box)

The issue is #5: I selected more than 3 options, so - if everything worked correctly - the value for the “Challenges (Java) - red box” column would be “0” (since at least 3 options were selected) - but it’s showing “1.”

The goal is to have a dashboard where all of these values are automatically calculated by the system (so I don’t have to export to excel/csv & manually calculate it.)


@KJK_14,

The script works. You can see an example here. Click Next to see that value of __js_Q20_Score.

I’m not sure how __js_Q20_Score maps to Challenges (Java). Are they really the same thing?


Weird - I tried to take your example but got the attached error message (“error”) - and it didn’t work.

 

Images attached for more context on the scoring...

  1. Q20 - the question I’m trying to figure out with the javascript - is mapped to the “Challenges (Java)” column in the dashboard (yellow arrows). I just re-named it because the actual question wording is really long. I want the dashboard to display the score (based on the selections) in that column for this question (Q20)
  1. The red box shows the value of the question (as it is now - which has the updated javascript in it.) I want to have the dashboard automatically calculate the “score” for all the items combined (blue box). So - if this worked as intended - both values in the “Challenges (Java)” column would be “0” (since I selected at least 3 options for each of those) - so the score in the “Sum” column for Test 1 would be 21 and Test 2 would be 19
  2. Excel version of the survey setup showing the type of question it is, the # of possible options, the minimum value, and the maximum value
    1. So - a "max” score for a completed survey would be 30 and the lowest score would be 7
    2. It’s an odd setup specifically because of this question (Row 6 - Item E) - since the minimum value is different (0 vs. 1) / the # of possible options doesn’t match the potential values / and this question involves “subtracting” values vs. just matching the value with the selection

 

 

 


Something weird happens if you follow the block preview link I posted above (probably attributable to New Experience).  Once you get to the page, click Restart and it works normally.

You should be using the embedded data field __js_Q20_Score in your sum instead of the question labeled Challenges (Java).


YAY IT WORKED.

You are a blessing.

But one more final twist - just out of curiosity - (and feel free to advise if it’s not possible due to the complexity/system setup…

Pic attached.

There are 18 current choices

  • Max value: 3
  • Min value: 0
  • If any of the top 17 are chosen: Then it takes 1 point from the “score” for each item selected (for a max of 3 points taken away)
  • If the bottom choice is chosen (red arrow in screenshot - “No challenges”) - then that should not subtract any points (as the goal is to have no challenges)
  • So it’s like - if the red arrow is chosen - then it records the max value of “3”
  • But if *any* other is selected - it takes 1 away
  • A workaround could be if the question is skipped/no selections chosen - then it counts as 3 points?

Is such a thing possible, or…?


Yes. You can do this:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
// Count how many checkboxes are checked
var selectedChoices = this.getSelectedChoices();
var selectedCount = selectedChoices.length;

// Cap score between 0 and max
var max = 3;
if(selectedChoices.includes("18")) selectedCount = 0;
else if(selectedCount>max) selectedCount = max;
var score = max - selectedCount;

// Store in embedded data
Qualtrics.SurveyEngine.setJSEmbeddedData("Q20_Score", score);
});

This assumes that the choiceid of the last choice is 18.  It usually is, but if you’ve added/removed choices it may not be.  If needed, change it to the correct value.


Magnificent.

 

I appreciate you, Tom!


Leave a Reply