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 trialSeth Warner
5,348 PointsI 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
var secret = prompt("What is the secret password?");
while (secret !== 'sesame') {
document.write('Thats wrong');
}
document.write("You know the secret password. Welcome.");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
1 Answer
Chyno Deluxe
16,936 PointsHey 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.
Seth Warner
5,348 PointsSeth Warner
5,348 PointsThanks Chyno Deluxe