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

Nathan Broaddus
Nathan Broaddus
5,875 Points

Why doesn't this work?

This seems logical to me. Why doesnt this work?

if the "var secret" is going to have the exact same value within the loop, then why do I have to assign it a value in order for the loop to work?

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


while (secret !== pass ){

   secret;

}


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

rydavim
rydavim
18,814 Points

The idea of the while loop in this challenge is to give the user several opportunities to guess the value of pass. You need to update the value of secret inside the while loop, otherwise the loop will run forever if the user doesn't guess the password correctly the first time.

while (secret !== pass ){  // If the user's guess is NOT the same as the password...
   secret; // You need to let them guess again here.
}

Hopefully that helps to clarify the challenge. Please let me know if it still doesn't make sense. Happy coding! :)

Robert Pazo
Robert Pazo
8,884 Points

By assigning a value to secret before the while loop and no way of updating secret in the while loop, the value assigned to secret remains the same as the initial returned value. Even though it seems as though you assigned the prompt function to secret, you actually assigned prompts return value to secret, which is why it will not display the prompt after the while loop begins. By placing secret = prompt("What is the secret password?"); inside the while loop you keep reassigning the variable to the value which the prompt function will return. Hope this helps :)

You code COULD work but you would need to swap the place of the prompt to inside the while loop.

var secret;
var pass = "sesame";


while (secret !== pass ){

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

}


document.write("You know the secret password. Welcome.");
Nathan Broaddus
Nathan Broaddus
5,875 Points

this may seem dumb, but to me it looks as if the prompt is inside the while loop.

That's because it is inside the while loop.

Nathan Broaddus
Nathan Broaddus
5,875 Points

then your first answer doesn't make any sense to me.

The variable secret will hold no value by default making the while condition equal to true and thus will run the prompt asking the user for the secret password.

It will continue to run until secret is equal to "sesame"