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 trialDaniel Hildreth
16,170 PointsNeed Help With While Loops
I need help with while loops; unless I'm shown howto exactly do something similar to what I'm asked then I don't know how to do it. In the code challenge of the JavaScript loops, it asks me to create a while loop that repeats until the user puts in the password 'sesame'. This is what I have so far:
var secret = prompt("What is the secret password?");
while ( secret = sesame ) {
}
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>
3 Answers
Daniel Newman
Courses Plus Student 10,715 Pointsvar secret = prompt("What is the secret password?");
var password = 'sesame';
while ( password = 'sesame' ) {
password = 'sesame' is assigning not compare. Use "===" or "!==" instead.
while ( password !== 'sesame' ) {
}
document.write("You know the secret password. Welcome.");
Jacob Mishkin
23,118 PointsDaniel you're really close on this one, just a couple of changes are needed. You do have the while loop set up correctly you are adding a variable that is not needed( var password). take a look at the code below:
var secret = prompt("What is the secret password");
while ( secret !== 'sesame' ) {
secret = prompt("What is the secret password");
}
document.write("You know the secret password, Welcome!");
the key here is the !== NOT operator, it states not equal too. so the loop is saying if the var secret is NOT equal to sesame run the loop. Here is a good reference on the while loop:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while
Daniel Hildreth
16,170 PointsThis is what I currently have in the app.js file.
var secret = prompt("What is the secret password?");
var password = 'sesame';
while ( password = 'sesame' ) {
}
document.write("You know the secret password. Welcome.");