Javascript 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 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 stucture 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);
  };
});

Be the first to reply!