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

Seth Warner
Seth Warner
5,348 Points

I know I have most of it already written out correctly, I'm just having a hard to with the whole variable = thing

Inside the while loop. If someone could show what I am missing and explain to me how it works, that would be awesome!

Seth

app.js
var secret = prompt("What is the secret password?");
while (secret !== 'sesame') {
document.write('Thats wrong');
}
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>

1 Answer

Hey Seth,

There are a couple ways you can do this but here is how I would do it to write out lest code.

var secret;
while (secret !== 'sesame') {
    secret = prompt("What is the secret password?")
}
document.write("You know the secret password. Welcome.");

I initialize the variable secret with no value. This causes the while loop to become true, becuase secret does not equal sesame, and then runs the loop which would prompt the question, "What is the secret password?".

Once a user enters the correct password of sesame, then any code after the while loop will run, in this case the document.write fires.

I hope this helps.