Reputation: 11
I'm having an issue where my counter variable is incrementing even though the statement should be evaluating to false.
The correctAnswers
variable continues to increment even when the else if
is incrementing the wrongAnswers
variable. The unanswered
variable also seems to double increment as though the statement is firing twice. I can't seem to isolate where the problem is occurring.
$("#quiz").on("click", function() {
if ($("input[name=question-1]:checked").val() === "correct") {
correctAnswers++;
unanswered--;
} else if ($("input[name=question-1]:checked").val() === "wrong") {
wrongAnwers++;
unanswered--;
}
if ($("input[name=question-2]:checked").val() === "correct") {
correctAnswers++;
unanswered--;
} else if ($("input[name=question-2]:checked").val() === "wrong") {
wrongAnwers++;
unanswered--;
}
console.log("correct " + correctAnswers);
console.log("wrong " + wrongAnwers);
console.log("unanswered " + unanswered);
});
Upvotes: 0
Views: 59
Reputation: 1888
I ran through the logic with every possible combination of true/false. Other than the fact that you're not counting unanswered questions as wrong answers, there was no unexplained increments of any variable. I would Console.log the initial values of each variable (before the if statements), and again between each if statement.
One other thing is to make sure you're using radio buttons and not checkboxes.
Fiddle: https://jsfiddle.net/TimothyKanski/y4upr5j8/
If you want to count unanswered questions as wrong, add an Else:
if (a) {
correctAnswers++;
unanswered--;
} else if (b) {
wrongAnwers++;
unanswered--;
}
else{
wrongAnwers++;
}
Upvotes: 1