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 trialKeith Greatz
4,377 PointsDo while loop challenge, lost, help please?
I've been asked to convert the following code into a do while loop.
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.");
I have changed it to the following
var secret = prompt("What is the secret password?");
do { 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.");
Im then told I only need the prompt once so I changed it to
var secret = prompt("What is the secret password?");
do { secret = prompt("What is the secret password?");
while ( secret !== "sesame" );
document.write("You know the secret password. Welcome.");
I know the third example is really stretching for an answer, the second looks right to me though, any help appreciated
Thanks Keiffy101
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.");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
3 Answers
Matt F.
9,518 PointsThey want you to declare the variable secret outside the loop, but without setting it to the prompt.
var secret;
do {secret = prompt("What is the secret password?"); }
while ( secret !== "sesame" ) {
}
document.write("You know the secret password. Welcome.");
Michael Oliver
7,855 PointsI don't understand the point of the var secret; Being a variable would "secret" not HAVE to hold something to be a variable. Whats the point if its empty?
Marcus Parsons
15,719 PointsIn do while loops, the code is executed in the code block before the conditional is checked. So, if you set secret
equal to a prompt when creating the variable, it would just be overwritten by the 2nd prompt that will immediately pop up from the do while loop.
Marcus Parsons
15,719 PointsOh, to answer your question, when you declare a variable without setting it equal to something, the variable exists but it's just undefined
until you assign it some type of value.
Michael Oliver
7,855 PointsThanks Marcus!
Marcus Parsons
15,719 PointsNo problem, man!
Keith Greatz
4,377 PointsKeith Greatz
4,377 PointsYou are a life saver!,
I was totally lost on why they were saying I was repeating the code, I can see now how the first var set up a condition for the while loop to follow, where the do function replaces the initial Var.
Thanks so much
Keiffy101
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsThe syntax is wrong here, though. There is one set of curly braces for a do while loop which come right after the do. The other set of curly braces after the while do nothing.