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

Loops and Arrays

Hi Guys,

I'm trying to create a quiz in Javascript. Here is my code:

///////////

var answers = [

['What colour am I thinking of?', "Red"],

['What number am I thinking of?', 'One'],

['What letter am I thinking of?', 'X']

]

do {

var questionOne = prompt(answers[0][0]);

}

while (questionOne === answers[0][1]);

//////////////

Why is it when I answer the question with the correct answer the loop does not end?

1 Answer

The test condition is:

while (questionOne === answers[0][1]);

This is going to only stop at a wrong answer, to have it end on the correct answer use this:

while (questionOne !== answers[0][1]);