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

scott Walker
scott Walker
2,506 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

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