Need to display a live update of the average of 6 inputted numbers | XM Community
Skip to main content

Hi,
I need to be able to display with live updates an average of 6 inputted numbers.
Basically, I need user to enter their ratings from 1-10 for 6 different questions and then I need the screen to display to them live what their average rating is.
Currently, I am using a "Constant Sum" question. And I can get coding to have the average display in the console log with what I've coded. But I'm having trouble figuring out how to get that to display on the actual screen.
The code I am using is below.
What I want to display (either above or ideally below) the constant sum/input area is:
"Your current average rating is [average]"
I need the [average] to update every time they enter something new (or as per my code on every click)

Qualtrics.SurveyEngine.addOnload(function()
{
var calculateAverage = function() {
currentChoices = document.querySelectorAll("inputtname^='QR~QID21']")
numberOfChoices = currentChoices.length
currentSum = 0
for(var i=0; i < numberOfChoices; i++) {
currentSum += parseInt(currentChoicessi].value)
}
currentAverage = currentSum / numberOfChoices
console.log("Current Average: " + currentAverage)
return currentAverage
}

var addBlurHandlers = function() {
currentChoices = document.querySelectorAll("inputtname^='QR~QID21']")
for (var i = 0; i < currentChoices.length; i++)
   currentChoicesci].onblur = calculateAverage  
}
addBlurHandlers()
  this.questionclick = function(event,element)
  {
calculateAverage();
}
});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
console.log("Page loaded...");
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});


You can take a look at the code here and its related demo here.
At present, the demo is based on a form type question, but it should also work on a constant sum question. With small modifications, you can easily, get it to do what you are looking for.


https://www.qualtrics.com/community/discussion/comment/33578#Comment_33578This worked perfectly even for the constant sum question! It still picked up the text entry to the constant sum question perfectly. So, all I had to do was add in a variable to calculate the average and then add in a line to display the average (very easy to do by just following the coding you provided). Plus I got the added bonus of the validation checks you provided by requiring more than 0 and less than the total average I needed.
Thank you for your help!


Leave a Reply