Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
210,438 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!