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

Eugen Oanta
Eugen Oanta
5,657 Points

Challenge solution which doesn't make sense

Below is my code which does not make sense for me but apparently it worked, out of frustration I have copy/pasted the code several times until it worked, still I don't get the whole process of how the page reads the code and prioritize it.

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

while (secret !== "sesame") {
secret = prompt("What is the secret password?");
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="app.js"></script>
</body>
</html>

2 Answers

Jennifer Hughes
Jennifer Hughes
11,421 Points

Hi Eugen,

The computer reads your code from top to bottom, so first it sets a variable you've called secret to equal a prompt. When the page loads, a prompt box will open and ask for the secret password. Whatever the user types into that prompt box is stored in the variable secret.

Next, the computer enters the while loop. While what's stored in the variable secret does NOT equal(!== means does not) the string "sesame", the program will continue to ask the user for the secret password. In your current code, in the while loop you have hard coded secret be set to "sesame". Therefore, secret DOES equal the string "sesame", and thus, the while loop will end.

Because the while loop has ended, the program will now read the document.write line, and it will write out the string you've coded.

However, I think you are after something slightly different. I think what you want is to prompt the user to enter the secret password until the user enters the word "sesame". To do this, you do not need to hard code the variable secret to be anything. Instead, simply set the variable secret to be whatever the user types in the prompt box:

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.");
Eugen Oanta
Eugen Oanta
5,657 Points

Thank you very much for your reply, I found it very detailed and carefully explained. It makes sense now. Thanks again :).