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

Björn Wauben
Björn Wauben
10,748 Points

Stuck on a while loop

After practicing while loops with random number generators I got to this challenge. Now I need to make a while loop with a string. This throws me off a little.

My code won't pass because I need to asign a new value to secret inside the loop. With the numbers you would write something like "var += 1;" and then the loop continued untill false. I think I need to do something similar with this string but can't figure it out by myself.

app.js
var secret = prompt("What is the secret password?");

while (secret !== "sesame") {
  prompt("try again");
   secret === "sesame"
  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>

2 Answers

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

You are very close. You are passing a new value to secret using prompt(). Your problem is that you have document.write inside of the while loop. If the correct password is entered it does NOT enter the while loop.

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

while (secret !== "sesame") {
  prompt("try again");
}

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

Hi LaVaughn,

This is closer! Inside the loop, secret needs to be assigned a new value from the prompt - otherwise the loop will never break, as secret is never changing.

You are so close!

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

while (secret !== "sesame") {
  prompt("try again");
   if ( /* your condition */) 
     document.write("You know the secret password. Welcome.");
}