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

Eden Gomez
Eden Gomez
22,565 Points

I don't understand why this code don't work! Please I need help

Please help!!

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

4 Answers

Juliana Demarque
Juliana Demarque
14,502 Points

In order to keeping it DRY (don't repeat yourself), I would just like to add a bit to Alexander's answer, changing the while loop to a do/while loop.

The difference is that you set the prompt method only once. It will execute the prompt method first thing, then, if the answer is wrong (!=="sesame"), it keeps on asking for the right password.

// There's no need to write this first line, 
// because we will be setting the secret var inside the loop
// - don't need this anymore - var secret = prompt("What is the secret password?");

// Set the secret var to use the prompt method
do {
  var secret = prompt("What is the secret password?");
} while (secret !== "sesame"); // ... and stay inside the loop until the answer is right

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

The goal of this challenge is to ask for the password until the person gets the password (which is sesame). Try this instead:

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

// While the input isn't sesame (if it is already sesame then this loop will do nothing)...
while (secret !== "sesame") {
  //  ...then keep asking for the password
  var secret = prompt("What is the secret password?");
}

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

Hope this helps! ~Alex

when I answer people's questions, I like to keep my answers nice and simple. Although when I'm actually programming, I always be as DRY as possible :)

~Alex

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

Hi there! While Alexander Davison provides a working solution, I'd like to provide an alternative that might be considered a little bit "DRY"er, if I may. :smiley:

var secret = '';

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

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

If we start secret equal to an empty string then it's by default not going to be equal to sesame. At this point the while loop will start and ask for the secret password until "sesame" is entered. It's not necessary to use var to redeclare the variable through every iteration of the loop.

Hope this helps! :sparkles:

Eden Gomez
Eden Gomez
22,565 Points

thank you so much guys!!