Skip to content
← Back to Community
How can I loop my quiz answers to get a final score
Profile icon
karlylamm1

So far I have declared the canidates name, canidates response, correct response, and created an input.question for questions 1-5. Now i am wanting to loop the answerKey to give a percentage result, but I am not sure how to make the system read the correct answers as numbers instead of strings. I want my final output to be let score='';
and have it read a percentage for the overall grade of the quiz. Can someone help me out with this?

Voters
Profile icon
mhmilyas
Profile icon
karlylamm1
Comments
hotnewtop
Profile icon
ash15khng

You can add a score variable at the top. Every time the candidate answers a question, you can use an if statement to check if the answer given is the same as the actual answer, if it is you can add 1 to score. Then at the end you can do score/<number of questions> to get the percentage.

let score = 0; // question 1 // .... // .... if (answer1 === candidateans1) { score++; } // other questions... // ... // ... console.log("Score: " + (score/5) + "%");
Profile icon
karlylamm1

Thank you! @ash15khng

Profile icon
ash15khng

@karlylamm1 No problem! Could you mark my answer as correct? Thanks!