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

J V
J V
3,414 Points

A 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
rydavim
18,813 Points

Your 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
J V
3,414 Points

Hi 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
rydavim
18,813 Points

You 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
J V
3,414 Points

I 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
rydavim
18,813 Points

That is an excellent summary, yes. :)

J V
J V
3,414 Points

Thank you for your help. I really appreciate it. :)