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 trialOliver Sewell
16,425 Pointsdo .. while loop , please can someone help im confused T__T
My brain hurts !?!?!?!? can some explain what ive done wrong @_@
/*var secret = prompt("What is the secret password?");
while ( secret !== "sesame" ) {
secret = prompt("What is the secret password?");
}
document.write("You know the secret password. Welcome.");*/
var secret;
do {
secret = prompt("What is the secret password?");
if (secret !== "sesame") {
prompt("sorry try again?");
}
} while (secret === "sesame") {
document.write("You know the secret password. Welcome.");
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
1 Answer
Tony Nguyen
24,934 PointsYoo waddup Oliver, sometimes it's best to go over the video to see what you did wrong. There are some things wrong with the following answer.
You do not need an if statement to check whether it should keep looping or not. The "while" will do that for us.
When you call a do while loop, it isn't two different statements.
First make it so we have a variable to work with.
var secret;
Then create your do while loop so that the prompt will at least be asked the first them whether the loop is false or not.
do {
secret = prompt("What is the secret password?");
} while ( secret !== "sesame" );
Then have this outside of your loop because we don't want this to be repeated, rather we want this to be displayed once the do while loop is finished!
document.write("You know the secret password. Welcome.");
Oliver Sewell
16,425 PointsOliver Sewell
16,425 PointsThankyou , this is an awesome answer explains it very well ! even though the "css" confused me at first xD