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 Simplify Repetitive Tasks with Loops Create a `do...while` loop

garryl torres
garryl torres
1,090 Points

is this code valid for my do/while loop?

i used the following code for this task, but the message shows that "the prompt should go inside the do..while loop". What am i doing wrong here?

var secret = prompt("What is the secret password?"); var pass = "sesame"; do { secret === pass; } while ( secret !== "sesame" ) { secret = prompt("What is the secret password?");
} document.write("You know the secret password. Welcome.");

garryl torres
garryl torres
1,090 Points

Here is another code i tried. I might have been closer with this one BUT still got an error saying to call the prompt only once.

var secret = prompt("What is the secret password?"); var pass = "sesame"; var userPass = false; do { secret = prompt("What is the secret password?"); if (secret === pass) { userPass = true; } }while ( secret !== "sesame" ) { secret = prompt("What is the secret password?");
} document.write("You know the secret password. Welcome.");

1 Answer

The syntax you want to use is:

do {
  code block to be executed
}
while (condition);

You know the code to be executed:

secret = prompt("What is the secret password?"); 

and the condition:

secret !== "sesame" 

The only thing remaining is to do as the challenge asks and declare secret before the loop. One of the error messages gives you the code:

var secret; 

Note in the declaration secret is not assigned the results of prompt. This is so you don't prompt the user twice before the response is checked.