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

Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

HOW?

How can i tell the secret is sesame!?

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

while (secret !== sesame){
   var sesame = "sesame";
   var 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>

2 Answers

your really close on this, just switch the empty var secret with the var secret the equals the prompt. you want the prompt to run inside the while loop. and delete the var sesame the you want the input from the var secret to equal sesame not a new var.

Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

Hi Jacob...

I wrote this:

while (secret !== "sesame"){

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

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

I have to strictly use the !==

It gives me this error:

Bummer! Don't use the var keyword inside the loop. You only need to declare the secret variable once, at the beginning of the script.

I just edited your response to view your code properly.

okay so first before we declare the loop we need a var called secret. then inside the loop we want to say while the input from the prompt does not say sesame loop again. So with what you have is really close the only thing you need to do is set the var secret as an empty var outside of the loop, then inside the loop have the var secret equal the prompt. remember outside you need to state var secret; inside the loop all you need to secret.

Not a problem, any time!

Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

Ah! Ok now I understood!

I have to declare the var outside the loop, than only "call" it inside, without declaring it.... i confused it with a function.

Thank you Jacob, very helpful!

Cesare