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 trialCameron Schroeder
10,799 PointsYou 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!");
2 Answers
Hans Gcn
3,827 PointsTry 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
11,125 Pointsthis was the correct answer, thank you
Mat T'Qartit
29,277 Pointslet 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
Zimri Leijen
11,835 PointsZimri Leijen
11,835 PointsIs this a question?
I assume you're asking why
doesn't work?
It's because of the extra
let
in your do. By havinglet secret = prompt()
instead ofsecret = prompt
you are making a new locally scoped variable calledsecret
(let's called itdo-secret
) which is different from the globally scopedsecret
(let's call itglobal-secret
). Instead of overwritingglobal-secret
it makes it owndo-secret
. So thesecret
in while will always stayundefined
, because that one refers to theglobal-secret
and not the locally scopeddo-secret
.To fix this, simply get rid of the
let
in front ofsecret = prompt()