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 trialJ V
3,414 PointsA Closer Look at Loop Conditions: What is my next step?
The problem seems simple, but I have hit a bump in the road.
This is what I have so far.
I ran the program in a web browser. If I type 'sesame' in the prompt window on the first attempt the program executes properly. If I type 'sesame' on the second, third, fourth etc. attempt, the program does not work.
My code is not being accepted and I cannot pass the Code Challenge. What may I be doing wrong?
var secret = prompt("What is the secret password?");
var guess = secret;
var counter = 0;
while(guess !== 'sesame')
{
prompt("What is the secret password?");
counter = counter + 1;
}
document.write("You know the secret password. Welcome.");
4 Answers
rydavim
18,814 PointsYour answer is very close. Remember that you need to update the value of the secret variable inside the while loop. Right now, your prompt isn't being saved, so the value of secret isn't changing.
It works if you enter it the first time, because you're saving the prompt the first time.
var secret = prompt("What is the secret password?"); // Your first line is saving the prompt. Save it inside your loop as well!
J V
3,414 PointsHi Rydavim,
I appreciate your help, but I have another question.
If I were to declare the variable secret outside of the loop as well, would it be used during the program or is it irrelevant?
var counter = 0;
var secret = prompt("What is the secret password?"); /*Is this variable irrelevant since it is not in scope?*/
while(secret !== 'sesame')
{
var secret = prompt("What is the secret password?");
counter = counter + 1;
}
document.write("You know the secret password. Welcome.")
rydavim
18,814 PointsYou only need to declare the variable with var once. So when you're inside the loop, you can use secret = prompt("What is the secret password?"); and leave off the var keyword.
In this case, it isn't irrelevant because they might get the answer right the first time, meaning that you'd never execute the code inside the loop. The other way to do this is to use a do while loop, which I believe you learn about a little bit later.
Hopefully that all makes sense, but let me know if it's still confusing. :)
J V
3,414 PointsI just want to clarify, from my understanding, Program #1 and Program #2 execute differently, but provides the same results?
/*PROGRAM #1*/
var counter = 0;
var secret = prompt("What is the secret password?"); /*line 2*/
while(secret !== 'sesame')
{
var secret = prompt("What is the secret passwordssss?"); /*line 5*/
counter = counter + 1;
}
document.write("You know the secret password. Welcome.")
When the code from Program #1 executes in the browser, the user is prompted from line 2 first:
var secret = prompt("What is the secret password?"); /*line 2*/
In this case, if the user answers correctly, the program will end and the while loop will not be executed. If the user enters the incorrect answer, then the while loop is executed, and the user is prompted from line 5:
var secret = prompt("What is the secret passwordssss?"); /*line 5*/
The user is prompted from line 5 and the while loop will execute until the user enters the correct password.
Now on the Program #2:
Here, the user is prompted from line 6 until the user enters the correct password. The while loop will run until the correct password is entered.
/*PROGRAM #2*/
var secret;
var counter = 0;
while(secret !== 'sesame')
{
secret = prompt("What is the secret password?"); /*line 6*/
counter = counter + 1;
}
document.write("You know the secret password. Welcome.");
Summary: It seems that both programs are similar, except that in Program #1, if the user enters the password correctly the first time, the while loop does not execute.
rydavim
18,814 PointsThat is an excellent summary, yes. :)
J V
3,414 PointsThank you for your help. I really appreciate it. :)