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

Gafur Iusupaliev
Gafur Iusupaliev
3,090 Points

Why doesn't this code work? It prompts the questions, but everything after the for loop does not work.

Here's the code:

var correctAnswers; var incorrectAnswers; var quiz = [ ['question', 'answer'], ['question?', 'answer'], ['best twitch emote?', 'Kappa'], ['best TTV emote?', 'OMEGALUL'] ];

for(i = 0; quiz.length > 0; i += 1) { var response = prompt(quiz[i][0]); if(response.toLowerCase === quiz[i][1].toLowerCase) { correctAnswers += 1; } else { incorrectAnswers += 1; }

}

document.write("You answered " + correctAnswers + "questions correctly"); document.write("You answered " + incorrectAnswers + "questions incorrectly");

That code is formatted with markdown. Click the Markdown Cheatsheet link right above the post answer button below.

2 Answers

You have two issues. The condition to run in your for loop should be:

for(i = 0; i < quiz.length; i += 1) {

quiz.length > 0 would create an infinite loop.

Second, toLowerCase should be followed by parentheses: toLowerCase()

Gafur Iusupaliev
Gafur Iusupaliev
3,090 Points

omg, i'm actually braindead, lol, I accidentally entered 0 insted of an "i", and thanks for pointing out the parenthesis, and how did you make your message look like an actual workspace, with the background and everything highlighted as needed

Gafur Iusupaliev
Gafur Iusupaliev
3,090 Points

Thanks again, sorry for the bad formatting