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

Help with this example please.

I am just looking for some help with the solution to this example. Also, any help with tips on knowing when to set variable/which variables to set prior to running the loop. Just having some difficulty putting it all together. Thank you.

app.js
var secret = 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>

1 Answer

Grace Kelly
Grace Kelly
33,990 Points

Hi Alicia, the challenge asks you to use a while loop to keep asking the user for the password until they provide the correct answer "sesame". We use a while loop because you set a condition and it will keep looping through the code until the condition has changed e.g the correct password has been provided. For this code challenge we use the while loop in the following way:

while ( secret !== "sesame" ) { //while the password is NOT equal to "sesame"
  secret = prompt("What is the secret password?");    //ask the user to provide a password
}

When the user provides the correct password ("sesame") the while loop will be exited and then the code following it will be executed, in this case:

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

Hope that helps!!