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

scott Walker
scott Walker
1,968 Points

do while loop

i think I'm getting stuck on where i should be putting my bullion for the code and what i should be putting in the { } for the do section

script.js
var secret = sesame
do ( prompt("What is the secret password?")) {   
} while (secret == true )
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

Ajay Prasannan
Ajay Prasannan
14,873 Points

Hi there, I hope these general points help out.

  • Doublecheck your first line. The JavaScript interpreter won’t know what sesame is. Without quotes it’ll try to find a sesame reference and find nothing! In my testing, it was okay to use var secret; to set up the variable without assigning a value.
  • The do statement should take the form do { ... }, with the stuff you want to happen on each loop iteration between the curly braces. In this case, that’s where the prompt should go.
  • The while condition can be left as it is in the challenge. In this case, all we want to see is whether, on each loop iteration, the saved prompt value is now equal to “sesame”.
  • The statement to display a success message can be placed after all the do/while logic, as it won’t run until someone enters the magic word into the prompt.

Putting this together, I got:

var secret;

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

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

For more on do/while, see:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while