Computing the score
- JavaScript course
- Beginner
- Lesson 80/133
- 5 min
Check the answer with if and increment the score counter.
The score accumulator
To keep the count of correct answers we use the accumulator pattern: we start at zero with let score = 0; before the loop, and on each correct answer we add one with score++.
score++ means the same as score = score + 1; it bumps the variable by one. When the loop ends, score holds the total number of correct answers.
Checking with strict equality
To check whether an answer is correct we use the strict equality operator ===. === compares both value and type; == does type conversion and can give surprising results.
The answer field on our question object is a number (for example 1). The value we got from parseInt is also a number. We compare them with ===: userAnswer === q.answer. If they're equal we bump score by one.
Practice Compute the score
Add a score accumulator to the previous lesson's code. Start it with let score = 0; before the loop. Inside the loop body, compare the userAnswer you got from parseInt with q.answer using ===; if they're equal, bump with score++. After the loop, print the result with console.log("Score:", score).
Starter code
// Define the quiz questions
const questions = [
{ question: "Capital of Turkey?", options: ["İzmir", "Ankara", "Bursa"], answer: 1 },
{ question: "Wettest city?", options: ["Rize", "Konya", "Ankara"], answer: 0 }
];
// User answers (option indexes as strings)
const userAnswers = ["1", "0"];
let answerIndex = 0;
for (const q of questions) {
const userAnswer = parseInt(userAnswers[answerIndex]);
console.log(q.question);
console.log("Your answer:", q.options[userAnswer]);
answerIndex++;
}
// Now compute the scoreSample solution
// Define the quiz questions
const questions = [
{ question: "Capital of Turkey?", options: ["İzmir", "Ankara", "Bursa"], answer: 1 },
{ question: "Wettest city?", options: ["Rize", "Konya", "Ankara"], answer: 0 }
];
// User answers (option indexes as strings)
const userAnswers = ["1", "0"];
let score = 0;
let answerIndex = 0;
for (const q of questions) {
const userAnswer = parseInt(userAnswers[answerIndex]);
if (userAnswer === q.answer) {
score++;
}
answerIndex++;
}
console.log("Score:", score);Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does the expression score++ do?
- It resets the score variable
- It doubles the score variable
- It returns the value of score without changing it
- It increases the value of score by one Correct answer
Explanation: score++ is the same as score = score + 1; it adds one.
2 Complete the if condition
if (userAnswer ____ q.answer) {
score++;
}
if (userAnswer === q.answer) {
score++;
}
Explanation: === is strict equality; it compares both value and type.
3 Match the expression to its meaning
| Term | Meaning |
|---|---|
let score = 0 |
Starts the counter at zero |
userAnswer === q.answer |
Checks correctness with strict equality |
score++ |
Increases the score by one |
console.log(score) |
Prints the score to the screen |