Question
The values can be displayed in completedHtml using {correctedAnswers} and {questionCount}
I would like to get these two numbers in the result too. Is it possible?
Tested page URL:
Test code
your_code_here
@jakobjh You saving the data into your database yourself. It means that you can modify it as you need. Here is the docs.
Thank you,
Andrew
@andrewtelnov Yes, i know that. But it would be easier if the number of correct answers AND the number of questions was already in the Result JSON - now that they are calculated in the script.
But I found the following code to be working (despite my poor Javascript skills) :
Survey.Serializer.addProperty("question", "correct_answers:number");
Survey.Serializer.addProperty("question", "no_of_questions:number");
function addExtraInfo (result) {
var questions = survey.getQuizQuestions();
var correct = 0;
var numberOfQuestions = 0;
questions.forEach(function(question) {
if(!question.isEmpty()) {
numberOfQuestions ++;
if(question.isAnswerCorrect())
correct ++;
}
});
result["correct_answers"] = correct;
result["no_of_questions"] = numberOfQuestions;
return result;
};
survey
.onComplete
.add(function (result) {
document
.querySelector('#surveyResult')
.textContent = "Result JSON:\n" + JSON.stringify(addExtraInfo(result.data), null, 3);
});
@jakobjh It is really severallines of code:
survey.onComplete.add(function (sender, options) {
var result = sender.data;
result["correct_answers"] = sender.getCorrectedAnswerCount();
result["no_of_questions"] = sender.getQuizQuestionCount();
//Post result into your database
});
Thank you,
Andrew
Ahh okay - great :)