Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Agnes Caringal
Agnes Caringal
6,239 Points

its all about, do...while loop...

i cant pass with this challenge

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

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

} while ( secret !== "sesame" ) {
  secret = prompt("What is the secret password?"); 
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

4 Answers

they want the prompt in the "do" brackets and the password accepted after the "while" condition. The way you have it currently, it will say the password is correct regardless of the input, since the condition on the do while loop always happens at least once. Basically, swap your 4th and 7th lines.

Benjamin Laughman
Benjamin Laughman
7,026 Points

I was using this same code block, and stuck on it for several hours, with the correct 'do' and 'while' statements, but, it didn't take until I deleted that top variable statement, and just left it as var secret; That way it doesn't run two prompts before accepting your answer and displaying the "You know the secret password. Welcome."

Benjamin Laughman
Benjamin Laughman
7,026 Points

For the record I was having trouble with this myself. Okay, so if you follow Mr. Black's advice, as I did, and swap your fourth and seventh lines, leaving all else the same, you get this:

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

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

} while ( secret !== 'sesame' )

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

The Challenge is going to count it as wrong, and say, "You only need to call the prompt() method once, and only inside the loop." I wasn't sure why this was coming up, so I put it all into Workspaces, and if you preview this block of code, it will ask you "What is the secret password?", and if you write "sesame" in the box, it will ask you "What is the secret password?" again. If you answer "sesame" a second time, it will then say, "You know the secret password. Welcome." So once I changed it to:

var secret;
do {

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

} while ( secret !== 'sesame' )

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

The program executed the prompt only once, and there was no problem with putting "sesame" in the first try and getting the Welcome response. It also will be accepted as correct by the Challenge in this format.

I believe that the idea is that you are just "declaring" the variable before the do...while statement, not actually ascribing any meaning to it outside of the function. That would make sense to me why it executed the prompt twice before accepting the word "sesame" as a correct answer. If you try it both ways in Workspaces it might make a whole lot more sense. Hope this helps.