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 A Closer Look At Loop Conditions

While loop question

So I got the challenge right by some mix of luck and intuition.

I understand all of the code except why this is necessary (why do you need to reset the variable secret with the prompt?)

secret = prompt();

Please help :)

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

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

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="app.js"></script>
</body>
</html>

4 Answers

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

I was going for D.R.Y. , not having to write out the statement again. I finally figured out when it was asking for new value to replace the initial, it was within the prompt(); only. Still this is not real clear on this, but I sort of get it. Lil intuitive with knowledge. :) How is my logic, moderator. :)

Thanks for posting this. I too assumed the prompt command alone, within the while loop, would assign the newly entered value to the variable "secret". The additional statement within the loop is apparently necessary to update the variable "secret", replacing what was stored when the prompt is initially called as the variable is defined. Good "intuition", as I was stumped till I saw your posting.

so I'm not the only one who got a bit confused

While technically your answer is correct, and by correct i mean it meets the requirements for the challenge to consider the answer passing. This is how I understood the question and how I answered it because it made most sense to me.

var secret;

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

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

I hope this helps.

That was who I wrote it initially, but the challenge instructed me that I 'Needed' to replace the value for the var secret inside the loop so I wrote that extra bit

secret = prompt();

While that might have been enough to pass the challenge, It would case an infinite loop and if the user didn't get the password correct the first time it would then prompt an empty prompt that would not update the secret variable.

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

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

D.R.Y. , declared the var secret before the loop, then I initialized the value within the do part of the loop, and there was no need to add code block for while part of the loop because the loop would end when the while condition hit false, the correct password.