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

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,164 Points

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.