Question T504
Visible to All Users

returning only correct answers

created 7 years ago (modified 7 years ago)

hello recently i asked a support question on getting custom values and the end of a survey the code posted is below

survey
.onComplete
.add(function (result) {
var totalScore = result.getAllQuestions().reduce(function(total, q) {
if(Array.isArray(q.choices)) {
return q.choices.reduce(function(s, c) {
if(q.value.indexOf(c.value) !== -1) {
s += c.points
}
return s;
}, total);
}
return total;
}, 0);
console.log(totalScore);
});

my question is how do i get only the questions that have been answered in this way as this seem to return all the questions even questions that we not selected. it also gives an error on visible if and nested visible if with indexof error.

Answers

created 7 years ago

Hello,
Use result.getAllQuestions(true); //returns visible only questions, https://surveyjs.io/Documentation/Library/?id=surveymodel#getAllQuestions

Thank you,
Andrew
SurveyJS Team

    Comments (2)

      hello andrew ,i am still getting every question value back eg… question has 3 possible text/values with custom points all i want at that point is the questions that were (visible/answered) and the answer they selected but when it comes through there i get all the possible answers regardless of boolean true/false set.

      so question asked > user selected yes 3:points > however what happens is it gets yes = 3 points, no= 0 points, maybe = 2 points.

      survey
      .onComplete
      .add(function (result) {
      var totalScore = result.getAllQuestions(true).reduce(function(total, q) {
      if(Array.isArray(q.choices)) {
      return q.choices.reduce(function(s, c) {
      if(q.value.indexOf(c.value) !== -1) {
      s += c.points
      <!-- here its adds up all the values from every possible answer which is incorrect, should be just the answer the user selected -->
      }
      return s;
      }, total);
      }
      return total;
      }, 0);
      console.log(totalScore);
      });

        Hello Andrew, your an absolute star, i noticed where the problem was coming from after setting true, i had another if going causing a mischef… thank you so much…