Question T2024
Visible to All Users

Count number of values

created 6 years ago

I have a survey with several yes/no questions using radiogroups. I'd like to count the number of "yes" results for each question (the value for yes is 1 and value for no is 0), then store it as a variable that I can later use to display on the results page (i.e. You have answered yes to X number of questions).

Any suggestions on how to do that?

Answers approved by surveyjs Support

created 6 years ago (modified 6 years ago)

Your code should be the following:

JavaScript
var questions = survey.getAllQuestions(); var yesCount = 0; var noCount = 0; for(var i = 0; i < questions.length; i ++) { var q = questions[i]; if(q.getType() != "radiogroup") continue; if(q.value == "yes") yesCount ++; if(q.value == "no") noCount ++; }

Thank you,
Andrew
SurveyJS Team