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

Jesse Fister
Jesse Fister
11,968 Points

The code in app.js just opens a prompt, asks for a password, and writes to the document. Something is missing.

I know this one is simple simple but I'm stuck on this one. The code in app.js just opens a prompt, asks for a password, and writes to the document. Something is missing. Add a while loop, so that a prompt keeps appearing until the user types "sesame" into the prompt. I understand the while loop

while ( ) {

}

I'm just not sure what to put in it. A hint would be great. Thanks.

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

while (  ) {

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


//while ( guess !== RandomNumber ) {
//  guess = RandomNumber( upper );
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>

13 Answers

Mark Long
Mark Long
15,763 Points

Remember that "the response from the prompt gets stored in the variable (var secret = response)." This means that "sesame" ought to be compared against the "response" which is the "var secret" variable. When the response does not equal the "sesame" password the expression evaluates "True" and the statement inside the while loop is executed. If it evaluates "False" the loop is broken and the next line of code is parsed.

In the loop below any answer other than "sesame" will cause the expression to evaluate "True" because the wrong answer is not equal (!==) to the right answer (it is "True" that the Response is not equal (!==) to the Password). That being the case the statement inside the while loop will execute and it causes a new prompt to occur requesting the secret password.

Once the user enters the correct password the expression will evaluate "False" because the correct password is equal to the correct password (it is "False" that the Response is not !== to the Password because the Response is equal (==) to the Password which make the expression evaluate "False.") and we jump the loop.

so...

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

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

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

Lindsay Groves
Lindsay Groves
4,811 Points

Thank for you this alternate explanation - it made a lot of sense to me!

Brilliant explanation, thank you very much

The answer posted by @Ali Amirazizi is incorrect and causes an infinite loop. It will pass the treehouse code challenge test for some reason, but if you run it in your browser it will create an infinite loop. It will not exit the loop even if you provide the correct answer 'sesame'.

Here is the correct answer

var secret = prompt("What is the secret password?");
while ( secret !== "sesame" ) {
  secret = prompt("What is the secret password?");    
}
document.write("You know the secret password. Welcome.");
Ali Amirazizi
Ali Amirazizi
16,087 Points

Thanks for the correction, I will delete my comment.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Chris.

I just tried your solution successfully. I would give you Best Answer but there's no such option under your comment. I've voted it up all the same.

Thanks

Sean :-)

Christian Solorzano
Christian Solorzano
6,606 Points

I don't understand this. Why is

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

underneath

while ( secret !== "sesame" ) {

Try this. You should check if password is "sesame"

while (guess !== "sesame")

P.S. put one more secret = prompt() inside while to check password again and again.

do{secret= prompt(“What is the secret password?”); } while(secret !== “sesame”) alert(“You know the secret password. Welcome!”)

I did it slightly different but it works.

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

while ( secret !== password  ) {
  secret = prompt("Secret password incorrect. Please try again.");
}

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

Thank you!!

Thanks for the help, guys. I was confused by having to add another var secret = ...

Jacques Simon
Jacques Simon
6,594 Points

hmm interesting. Thanks guys!

thanks you so much guys!.

I am very confused. What is the reason to have another variable while we have a text name "sesame"???

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

while ( secret !== password ) { secret = prompt("Secret password incorrect. Please try again."); }

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

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

var count = 0 while ( secret !== password ) { secret = prompt("Secret password incorrect. Please try again."); }

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

"This was my answer because it was stated for only once"

Here is another way of doing it.

let secret = "sesame";

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

This would be the correct answer