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

do...while.... loop challenge.

I am trying to do the second do while loop challenge. I seem to have hit a standstill. I am utilizing brackets, because i kept receiving a syntax error/parse error, and thought I may be missing a semi colon somewhere. But as i run my code, I get a continuous loop of the "what is the secret password" prompt.

script.js
var secret;
var correctPassword = false;
do {
  secret = prompt("What is the secret password?");
  if (secret === "sesame") {
      correctPassword = true;
      document.write("You know the secret password. Welcome.");
  }
while ( secret !== "sesame" ) {
  secret = prompt("What is the secret password?");    
  }
}
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

1 Answer

Tobias Helmrich
Tobias Helmrich
31,603 Points

Hey James,

you were on the right track but the problem is that the while in a do-while loop doesn't have a body with code. You have to close the do loop before the while and remove the code in the body of the while. The while is just there to check the condition, not to execute code and even if it would work you would prompt the user for his input two times which is obsolete as it is a loop. I hope that makes sense, if you have further questions feel free to ask! :)

Here is your code modified so it will work:

var secret;
var correctPassword = false;
do {
  secret = prompt("What is the secret password?");
  if (secret === "sesame") {
      correctPassword = true;
      document.write("You know the secret password. Welcome.");
  }
} while ( secret !== "sesame" )

Good luck! :)