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

I can't figure out the while statement. Can someone assist me?

There is something wrong with the way I'm writing this code. What is it? Please

``` javascript
var secret = prompt("What is the secret password?"); {
var sesame = secret;
document.write("You know the secret password. Welcome.");
while (secret !== sesame) {
  document.write("Something is missing.");
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
<script src="app.js"></script>

1 Answer

Hi there Michael, There are a few things wrong with your code. Let me explain.

The challenge:

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.

The reason your code above cant complete the challenge is because:

  1. The prompt is not in the loop, and therefore wont continue to pop up until the users enters the correct secret.
  2. When someone types something in to the prompt, it goes in to our program as a string. So, while we are using our comparison we need to make sure our word is in double or single quotes. For example: "sesame"
  3. We don't need var sesame = secret; We are using a !== comparison operator in our while loop, so as long as it does not = our secret "sesame" it will not pass. On top of that since sesame is a var and not a string we cant compare it.

So what we do is:

var secret = prompt("What is the secret password?"); //setting the input value to the variable secret

document.write("Something is missing.")

while (secret !== "sesame") {
       secret = prompt("What is the secret password?"); //checking to see if it is right, if not... do it again
document.write("You know the secret password. Welcome.");// when the password is correct

So, we finished the challenge. I hope this helps you understand where you got stuck, and helps you move forward in your coding goals.

Thanks so much Casey! It's beginning to make since.