Score calculation
- Node.js course
- Intermediate
- Lesson 45/116
- 6 min
Learn correct count with the score calculation method.
Score calculation with score
Now we add a score method to the Quiz class. This method takes an array of answers, compares each to the correct answer and returns a result. Example: score(answers) { let correct = 0; ... return { correct, total, percentage }; }.
You take the user's answer to the i-th question with answers[i]. You compare it to the correct answer with this.questions[i].answer. If they match, correct increases by one. Then percentage = Math.round((correct / total) * 100) computes the percentage. Math.round rounds the result to the nearest integer. The returned object has three properties: correct (correct count), total (total questions), percentage (percent). This is the summary of the quiz result.
Rounding with Math.round
Math.round(x) rounds a number to the nearest integer. Math.round(4.4) gives 4, Math.round(4.6) gives 5. In score calculation (correct / total) * 100 is usually a fractional number; Math.round turns it into an integer.
Example: if 2 out of 3 are correct, (2/3) * 100 = 66.666.... Math.round turns it into 67. This gives a meaningful integer when showing the percentage to the user. Math.floor always rounds down, Math.ceil always rounds up. Math.round goes to the nearest; this is the most suitable for percentage display. The result object is returned as { correct, total, percentage }.
Practice Add the score method
Take the previous lesson's solution. Add a score(answers) method to the Quiz class: start with let correct = 0;, in a for loop do if (answers[i] === this.questions[i].answer) { correct++; }, finally const percentage = Math.round((correct / total) * 100); return { correct: correct, total: total, percentage: percentage };. The last lines are ready: const result = quiz.score(["Ankara", "Izmir"]) and two console.log calls.
Starter code
class Question {
constructor(id, text, answer) {
this.id = id;
this.text = text;
this.answer = answer;
}
}
class Quiz {
constructor() {
this.questions = [];
}
addQuestion(text, answer) {
const id = this.questions.length + 1;
this.questions.push(new Question(id, text, answer));
}
run() {
for (let i = 0; i < this.questions.length; i++) {
console.log("Question " + this.questions[i].id + ": " + this.questions[i].text);
}
}
// Add the score method
// write here
}
const quiz = new Quiz();
quiz.addQuestion("Capital?", "Ankara");
quiz.addQuestion("Largest city?", "Istanbul");
const result = quiz.score(["Ankara", "Izmir"]);
console.log("Correct: " + result.correct);
console.log("Percent: " + result.percentage);Sample solution
class Question {
constructor(id, text, answer) {
this.id = id;
this.text = text;
this.answer = answer;
}
}
class Quiz {
constructor() {
this.questions = [];
}
addQuestion(text, answer) {
const id = this.questions.length + 1;
this.questions.push(new Question(id, text, answer));
}
run() {
for (let i = 0; i < this.questions.length; i++) {
console.log("Question " + this.questions[i].id + ": " + this.questions[i].text);
}
}
score(answers) {
let correct = 0;
for (let i = 0; i < this.questions.length; i++) {
if (answers[i] === this.questions[i].answer) {
correct++;
}
}
const total = this.questions.length;
const percentage = Math.round((correct / total) * 100);
return { correct: correct, total: total, percentage: percentage };
}
}
const quiz = new Quiz();
quiz.addQuestion("Capital?", "Ankara");
quiz.addQuestion("Largest city?", "Istanbul");
const result = quiz.score(["Ankara", "Izmir"]);
console.log("Correct: " + result.correct);
console.log("Percent: " + result.percentage);Expected output
Doğru: 1 Yüzde: 50
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does score(["Ankara", "Izmir"]) return on a two-question quiz?
quiz.score(["Ankara", "İzmir"])- correct: 1, percentage: 50 Correct answer
- correct: 2, percentage: 100
- correct: 0, percentage: 0
- correct: 1, percentage: 25
Explanation: One answer is right (Ankara), one is wrong (Izmir). 1/2 = 50%.
2 Match each piece to its role
| Term | Meaning |
|---|---|
answers[i] |
User's answer |
questions[i].answer |
Correct answer |
correct |
Correct count |
percentage |
Percent |
3 Complete the percentage calculation
const percentage = Math.____((correct / total) * 100);
const percentage = Math.round((correct / total) * 100);
Explanation: Math.round rounds to the nearest.