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 trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Alexander Rizov
Alexander Rizov
4,910 Points

Answers are not counted properly. Help!

Ok, I realise I'll be kicking myself later for not figuring it out, but right now I'm at the end of my rope.

When the code runs it always counts 0 right answers and 1 wrong, even when I type nothing in the prompt window. The questions all appear in order and I'm not getting any error messages in the console.

Here's a link to my workspace snapshot: https://w.trhou.se/lce3j4v85i

Thanks in advance!

1 Answer

Steven Parker
Steven Parker
231,275 Points

The variables "correct" and "wrong" are created and set to 0 inside the loop, so they will only contain the result of the last question asked. Move the declarations out of the loop to make them global.

Also note that the "result" is being compared to the "question" instead of the "answer".

Alexander Rizov
Alexander Rizov
4,910 Points

Haha, that the responce was compared to the question made me facepalm, but the fact about the correct/wrong variables not being global would have not occured to me. I guess I'll be reading more on scope tonight.

Thank you for your help!