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

re-write code using do while loop

it gives me a parse error . what is wrong here ? and also is there a simple way i can do this other than this...i just used the method in the video ...but honestly i don't understand why we have to write it in this complicated way using a do while , when we can just do it while...

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.");

var correctSecret =false; 
var secretCount= 0;

do { 
  secret = prompt ( "what is the secret password?");
    secretCount +=1 ;
  if (secret == "sesame") { 
   correctSecret = true; 
  } 

} while ( !correctSecret ) 
  document.write ( " you know the secret passowrd. welcome."; 
                  document.write( " it took you" + secretCount + "to guess the correct password" + secret); 
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

Asa Smith
Asa Smith
10,009 Points

Your code is good, once you add the ')' that Yonatan Arbel pointed out. Just delete or comment out the code that was provided when you opened the exercise.

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.");

thank you

Yonatan Arbel
Yonatan Arbel
7,055 Points

you just miss one parentheses at the "document.write" ( document.write ( " you know the secret passowrd. welcome.";

oh thanks ...i thought there is a simpler way to write it and i have complicated it though... is the way i have written correct

NO it still doesn't work . It says " bummer . use the prompt () method only once inside the loop. and define var above the loop " . I just don't get it