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

Taqwa R
Taqwa R
1,852 Points

I'm stuck - what is wrong with my do while loop?

I tried to follow an example from the lesson as a model to create the do while loop for this challenge task. I can't figure out what I'm missing.

script.js
var secret;
var correctGuess =  false;
var guessCount = 0;

do {
  secret = prompt("What is the secret password?");
  guessCount += 1;
  if (secret === 'seasame') {
    correctGuess = true;
  }
} while ( correctGuess = true )
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>

2 Answers

Josh White
Josh White
3,630 Points

I believe that since you have the while() statement looking for correctGuess to be true that is where your running into an error. Basically what it is looking for is Hey while the correctGuess variable is true I want you to do this. If you change the while() to be while(!correctGuess) it will only continue to run the do loop until the variable to changed to true. It's just backwards. I'm assuming if you enter the wrong password the program will stop.

Taqwa R
Taqwa R
1,852 Points

That doesn't seem to be the answer. I tried changing it it to ( ! correctGuess ) but I'm still getting the error message. :-/

Sipann A.
Sipann A.
30,234 Points

I think Josh is right. Your updated code (with while !correctGuess)) works fine here. Just to be sure, are you entering the correct password (since you have specified 'seasame' and not 'sesame') ?