Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKevin O'Brien
3,802 PointsAnswers Don't Increment
Hi all,
I am trying to resolve with the following code, but somehow, I think my answers do not increment at correct or wrong ones.
I have used slightly different for loop, with prompt, but cannot understand where I am exactly failing. Here is the code
var quiz = [
["What's your name?", "Kevin"],
["Where do you live?", "Dublin"],
["What is your favourite food?", "Sushi"]
];
var answer;
var correctAnswer = 0;
var wrongAnswer = 0;
var html;
function print(message) {
document.write(message);
}
for(var i = 0; i < quiz.length; i += 1) {
answer = prompt(quiz[i][0]);
if (answer === quiz[i][1]) {
correctAnswer =+ 1;
else
wrongAnswer =+1;
}
}
html = "You have answered " + correctAnswer + " questions correctly but you have answered " + wrongAnswer+ " questions wrongly.";
print(html);
I could not understand the exact reason of Dave having the loop part as questions, so if anyone can assist on that, it would be very helpful.
Thanks
- Kevin
3 Answers
Gabriel Roberto Moreira Santos
1,182 PointsIf you're speaking about the "correctAnswer" and "wrongAnswer", the correct form to increment these numbers are [ correctAnswer += 1 ] or [ correctAnswer++ ] and not [ correctAnswer =+ 1 ].
Kevin O'Brien
3,802 PointsThanks a lot, Gabriel!
I was surprised to see that it was such a basic mistake and slipped through my attention and made me think if the logic is flawed at the loop. Also, thank you for the rapid response!
Also noticed I made a small mistake at "quiz.length", which I found after looking to where you mentioned, regards!
Ellis Corio
22,035 PointsAlso it would help if you used answer.toLowercase and stored the answers in your array as lowercase, that way the answers are no longer case sensitive.