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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a `do...while` loop

Keith Greatz
Keith Greatz
4,377 Points

Do 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

script.js
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.");
index.html
<!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.
Matt F.
9,518 Points

They 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.");
Keith Greatz
Keith Greatz
4,377 Points

You 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

The 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.

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


document.write("You know the secret password. Welcome.");
Michael Oliver
Michael Oliver
7,855 Points

I 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?

In 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.

Oh, 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.

No problem, man!