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

Leno Marin
PLUS
Leno Marin
Courses Plus Student 1,323 Points

Wouldn't this run 'while' code block anyway?

In this code:

var secret = '';

do { secret = prompt("What is the secret password?"); }while ( secret !== "sesame" ) { document.write("You know the secret password. Welcome."); }

Wouldn't the while run the first time around whether it's true or false?

script.js
var secret = ''; 

do {
  secret = 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>

5 Answers

Sam Donald
Sam Donald
36,305 Points

A while loop

while( condition-to-test )
{
 // Do some funky code.
}

This funky cod may not ever run because the condition-to-test could validate on the first pass.

A do-while loop

do
{
 // Some funky code
}
while( condition-to-test );

This funky code will definitely run at least once because the condition-to-test has not yet been assessed.

For your code

The user will continually be prompted to input the password until they input sesame.

The document.write line will not run until secret == sesame, because you never leave the loop until then. And thus no code below it can run.

Also, for your code

do {
  secret = prompt("What is the secret password?");
} while (secret !== "sesame") {//Get rid of this bracket
  document.write("You know the secret password. Welcome.");
}//Get rid of this bracket

You don't need those two curly brackets.

Leno Marin
PLUS
Leno Marin
Courses Plus Student 1,323 Points

Okay. But I thought the whole point is to not document.write "You know the secret password. Welcome" until the prompt is "sesame". But if I understand correctly, the text will write regardless the first time around. Am I understanding that correctly.

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you can move the print out of the while block and still pass the challenge. i tried it both ways, both passed. in real life of course you wouldn't print welcome until the password had been entered, so that would be out of the while block, whereas in the while block like you have, i think would print every time but i don't have an ide to verify.