JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a `do...while` loop

Gedeon Bashimbe
Gedeon Bashimbe
1,023 Points

Can somebody please help me with my code.

Not sure on how to fix this.

script.js
var secret = prompt("What is the secret password?");

do {
 prompt("What is the secret password?"); 
} while ( secret !== "sesame" ) {

}
document.write("You know the secret password. Welcome.");
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
Greg Nemes
Greg Nemes
3,499 Points

I think the issue comes from declaring the secret variable outside of the do loop. This does two things. First, it calls the prompt function, which you do not want to happen before the do loop. Second, it means that the users response to the prompt you call inside the do loop is not stored in a variable, and then it cannot be compared in the while condition. I would suggest replacing your fourth line with your first line.

Also, you should not have curly braces after the while ( secret !== "sesame" )

James Braun
James Braun
Full Stack JavaScript Techdegree Graduate 31,472 Points

I believe you have to say secret=prompt("what is the secret password?") in the do loop. so that srcret can be assigned a value

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 144,286 Points

Hi Gedeon Bashimbe

You're on the right track and super close.

For the most part, everything is correct and you have all the pieces, it just needs a little fix. First, outside the loop, you should only declare the secret variable... don't assign anything to it here. Remember, there are two parts to using a variable: 1. Declaring it (eg. var secret;) and 2. initializing it or assigning it a value (eg. secret = prompt...)

For this challenge, you'll want to declare the variable outside the loop, but initialize it inside the loop.

I think you'll get it now! Nice work. :)

:dizzy: