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

S Ananda
S Ananda
9,474 Points

Help with do... while loop

All it said was try again. Don't know what I'm missing can someone help me see my mistake?

''' var secret; /* Secret password*/ var correctGuess = false; /Runs when they guess the correct answer/

do { secret = prompt("What is the secret password?"); if(secret === correctGuess){ correctGuess=true; }

} while (! correctGuess){ document.write("You know the secret password. Welcome.");

} '''

Thanks

script.js
var secret; /* Secret password*/
var correctGuess = false; /*Runs when they guess the correct answer*/

do {
  secret = prompt("What is the secret password?");
  if(secret === correctGuess){
    correctGuess=true;
  }

} while (! correctGuess){
    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>

1 Answer

Hello, looks like you just need to fix up the syntax a little, remember to check for "sesame" instead of correctGuess and move the document.write() up into your if block. Here is an example.

var done = false;
var secret;

do {
  secret = prompt("What is the secret password?"); 
  if (secret === "sesame") {
    document.write("You know the secret password. Welcome.");
    done = true;
  }
} while (!done)
S Ananda
S Ananda
9,474 Points

Thanks Jeremiah, so much. Sometimes I just can't see simple things, like leaving out "sesame." Got it to run and understand my error, so am good to go.