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

Why is this not working?

The error says Bummer: You only need to call the prompt() method once, and only inside the loop. Hint: Declare the secret variable before the loop.

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

} 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>

1 Answer

Valerio Ripamonti
Valerio Ripamonti
4,488 Points

The prompt method returns what's typed by the user (the password), you can store it the variable 'secret'. I've declared the variable secret outside the loop, in this way the variable is declared (created) only once. Using a do-while loop a prompt will appear at least once and will always appear until the password is correct.

var secret;

do {

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

} while ( secret !== "sesame" )

document.write("You know the secret password. Welcome.");

Have Fun and happy coding!