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

Cameron Schroeder
Cameron Schroeder
10,799 Points

You don't always need to use a counter or specify an exact number of times that a loop must run. Javascript Loops

You don't always need to use a counter or specify an exact number of times that a loop must run. All you need is a condition that evaluates to false at some point so that the loop can end.

The code in script.js opens a prompt dialog that asks for a password and assigns it to the variable secret. It also displays an alert dialog. Currently, no code checks the password.

Add a do...while loop that keeps displaying the prompt dialog until the user types 'sesame'.

here's my answer that it's not accepting:

// Display the prompt dialogue while the value assigned to secret is not equal to "sesame" let secret; do { let secret = prompt("What is the secret password?"); } while (secret !== 'sesame');

// This should run after the loop is done executing alert("You know the secret password. Welcome!");

Is this a question?

I assume you're asking why

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

alert("You know the secret password. Welcome!");

doesn't work?

It's because of the extra let in your do. By having let secret = prompt() instead of secret = prompt you are making a new locally scoped variable called secret (let's called it do-secret) which is different from the globally scoped secret (let's call it global-secret). Instead of overwriting global-secret it makes it own do-secret. So the secret in while will always stay undefined, because that one refers to the global-secret and not the locally scoped do-secret.

To fix this, simply get rid of the let in front of secret = prompt()

2 Answers

Try this -

// Display the prompt dialogue while the value assigned to `secret` is not equal to "sesame"

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


// This should run after the loop is done executing
alert("You know the secret password. Welcome!");```
a s
a s
11,125 Points

this was the correct answer, thank you

let secret; do { secret = prompt('what is the secret password'); } while ( secret !== 'sesame' ); // This should run after the loop is done executing alert("You know the secret password. Welcome!");

This worked for me