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

James Cobbett
James Cobbett
1,468 Points

Not sure what i have done wrong

Im not sure what i have done wrong on this one. Maybe I have it in the wrong order?

script.js
var secret;
prompt("What is the secret password?");
do {
prompt("What is the secret password?");
document.write("You know the secret password. Welcome.");
}
while (secret !== "sesame") {  
}
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

John Lack-Wilson
John Lack-Wilson
8,181 Points

You're pretty close!

Looks like there might be a syntax problem with your do/while loop. Check out this link to see how the syntax of a do/while loop.

Antti Lylander
Antti Lylander
9,686 Points
var secret;
prompt("What is the secret password?"); //what is the purpose of this line?
do {
prompt("What is the secret password?");
document.write("You know the secret password. Welcome."); //this is now printed every time the code inside the loop is done. Move it after the loop so that it will be run when the loop has finished.
}
while (secret !== "sesame") {  //remove these curly braces
}
Alan Lopez
seal-mask
.a{fill-rule:evenodd;}techdegree
Alan Lopez
PHP Development Techdegree Student 6,830 Points

Hi James,

It took me a little to understand this as well, but I was able to find the answer here: https://teamtreehouse.com/community/i-dont-understand-58

I took your code and added comments to maybe help you understand what was wrong with it. A little long but I hope it helps with understanding.

var secret;  //Declare the variable

prompt("What is the secret password?");  //You will only want to use the prompt inside the do while loop, so delete this from here.  

do { 

prompt("What is the secret password?");  //this is where you can re-declare your "secret" variable, along with the prompt. So it should read like this: 

secret = prompt("What is the secret password?");   //Only use the prompt once inside the loop, so that it can loop over and over again until the condition ("while") is met.  

document.write("You know the secret password. Welcome.");  //this will go outside of the loop, because this is the confirmation that the condition of the loop (secret !== "sesame"), was met and can now finish looping. Delete from here and move to outside of the loop. 
}

while (secret !== "sesame") //This is the condition to be met. Don't forget your ";" . 
{ //you can delete these  
}//delete 

//add confirmation here