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

Why my answer didn't work? (While Loop)

So, it said "Bummer"..

But when I tried my code on the Console, it works!. It loops until it gets the password "sesame", then print it out something.

Where did I got it wrong? Thankyou

app.js
//var secret = prompt("What is the secret password?");
while ( secret !== "sesame") {
  var secret = prompt("What is the secret password?");
}
if ( 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

You have a couple of issues going on, and it's mostly due to the challenge it's self. first you need to have the prompt appear prior to the while loop, so you need to not have that commented out. second you need to remove the if statement as there is no need for it to complete the challenge. its asking to repeat the prompt if not sesame. the challenge reads this as false because its not needed for the challenge to work. after the while loop have the document.write function the same, and after that you should be good to go.

yes, thankyou very much!

Joseph Taylor
Joseph Taylor
5,128 Points

I don't actually believe there is an issue with the way you've coded it, but according to the automated hint, the var keyword should only be used once, at the top of the file. Including the var keyword in a loop means the variable gets declared (as distinct from merely being assigned) over and over again, which, while not an issue in practice, isn't much loved by the linter. So, it really boils down to a stylistic issue, but what you're looking for is something like this:

var secret;

while (...) {
  secret = ...;
   ...
}

...

yes, thankyou!