Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look At Loop Conditions

It keeps on repeating "Try again"

I made it strictly equals equalsto check the data in the variable it holds. But still nothing to return.

app.js
var secret = prompt("What is the secret password?");
while(secret === 'sesame'){
  alert(secret);
}
  document.write("You know the secret password. Welcome.");
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
Lee Cockcroft
Lee Cockcroft
5,147 Points

HI

try the code below:

I am also a beginner, (so there may be a better alternative)

this below is saying when the answer is incorrect keep asking to try again, as soon as the correct answer is given the loop breaks.

Again, sorry if this doesn't answer your question, as i am a beginner, I try to debug these as it helps me learn.

I hope my answer helps

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JavaScript Loops</title> </head> <body>

<script type="text/javascript"> var secret = prompt("What is the secret password?"); while(secret !== 'sesame'){ var tryagain=prompt("that is incorrect please try again"); if(tryagain==="sesame") { break;

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

</script>

4 Answers

Bryan Peters
Bryan Peters
11,996 Points

Nothing inside your loop changes the condition, so if you were to actually run this as written and enter "sesame" when prompted, the while loop would repeat infinitely.

So,

  1. The prompt needs to be inside the loop, so that the value of secret can change with each iteration.
  2. The loop should repeat as long as the response is NOT equal to sesame. The goal is to keep repeating the prompt until they give you the correct response.
  3. You don't need the alert inside the loop

Bryan Peters its still the same and it didnt work.

Bryan Peters
Bryan Peters
11,996 Points

re-post JS code please.

There are a number of ways to do this. Here is another approach:

while(prompt("What is the secret password?") !== 'sesame'){
   alert("Sorry,  try again");
}
document.write("You know the secret password. Welcome.");

Bryan Peters Alena Holligan I did try it like you did and I took away the alert as you said

while(var secret = prompt("What is the secret password?") !== 'sesame'){ } document.write("You know the secret password. Welcome.");

Jennifer Nordell and Liam Walls I just can get this exercise to pass through

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! It looks like you're redeclaring your variable inside your while, which was not the intention. Also, there are no statements inside your while loop to get a new value for secret. Here was my solution:

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.");

Here we ask for the secret password. If it's not equal to "sesame" we start the while loop which asks for the secret password. This continues until the user enters "sesame". Upon the user entering "sesame" the while loop exits and the welcome message is written to the DOM.

Good luck! :sparkles: