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 while loop challenge

Challenge Task 1 of 1

Can someone help tell me why this code below does not work?

(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 promp)

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

while (secret !== password) {
  prompt("What is the secret password?");
}

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

Many Thanks

3 Answers

You're not setting the secret variable in the loop, so the loop will always run, no matter what. This should solve your problem:

// You don't have to prompt for the password at first. You can wait for the loop
var secret = null;
var password = "sesame";
while (secret !== password) {
  // Notice how you have to assign to the secret variable here
  secret = prompt("What is the secret password?");
}

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

Michael, I struggle some with loops myself...so my follow up question is could you solve this a different way by invoking (vocab?) the secret var itself vs. secret equaling what the program already knows secret is assigned to?:

var secret = null;
var password = "sesame";
while (secret !== password) {
  (run) secret //I don't know how you'd say run secret, but do you 
                     //really need to include the info inside the prompt? 
}

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

secret is a string, so it's not callable like a function is. You can compare it to another string, though, like you do in the parentheses of the while loop. You include the phrase "What is the secret password?" inside the prompt, because the prompt function needs to know what to ask the user

Ah, ok the string comparison makes sense. Thanks for the response and clarification Michael!

I'm struggling with this challenge as well. Why does var secret = null?

secret can be anything you want it to be, initially. I like null, because that signifies the intentional absence of a value, which is intended functionality until it's set in the loop. You could also just declare the variable and leave it be (it'd be undefined then), or you can assign it to anything you want, but it'll get set every time the loop runs, so whatever it starts out as doesn't matter (as long as it doesn't start out as the password)

I think this would solve your problem.Try this out. You declare a variable secret and then ask for a password and it will keep on bugging the user unless he fills in the right password.Or you can use do while loop here as well if you want to.

var secret ;
var password = "sesame";

while (secret !== password) {
 secret = prompt("What is the secret password?");
}

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

Thanks everybody for the quick response, makes a bit more sense now.

Glad we could help, just think of WHILE loop as its keeps on repeating a specific thing until it meets a certain condition and in your eg. It will keep on bugging the user unless he enters "sesame"(that's your condition when while loop should stop) and prompt is the action it will keep on doing until the condition is met