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

Froi Nunez
Froi Nunez
12,133 Points

While loop challenge

I'm not sure why it isn't working. It tells me " I should assign a new value to the secret variable inside the loop".

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

while (secret !== "sesame") {
      prompt("What is the secret password?");
}

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

Right now you test the input from the user once, you set the variable the first time they enter an attempt at the password. However you have no code to reassign the variable within the loop if they try again. Right now your loop will last forever because the value of secret will never change.

You don't even have to write more code at this time - you just have to refactor it a bit.

Froi Nunez
Froi Nunez
12,133 Points

I understand what you mean, but I'm having trouble actually applying the syntax. i have something like this now:

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

while (secret !== "sesame") { prompt("What is the secret password?"); if (secret === "sesame") { answer = true; alert("Correct password!"); } }

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

This still hasn't worked.

Froi Nunez
Froi Nunez
12,133 Points

OOhh got it! that was way more simple than I thought. Thank you.

Froi

Like I said earlier - you don't have to write any new code - you just have to refactor (move stuff around)

Think about the variable you are testing, and how that variable is set. Should it be set statically once or dynamically?

(Let me be clear, that I don't think just giving someone the answer when they have a problem on the code challenges does them any good)