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

sakchai charoensin
PLUS
sakchai charoensin
Courses Plus Student 1,112 Points

I'm stuck at the "do...while" loop challenge.

Could someone tell me what is wrong with my code?

script.js
var secret ;
var correctPass = false ;
var passWord = 1234 ;
do{
  secret = prompt("What is the secret password?");
  if( parseInt(secret) === passWord ){
     correctPass = true ;
     }
}while( !correctPass )
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

Piotr Manczak
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Piotr Manczak
Front End Web Development Techdegree Graduate 28,940 Points

Try this:

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

Hey sakchai,

Your answer looks correct. I tried it in the console and it seems to be working as expected. Have you tried restarting the challenge or refreshing the browser? Make sure you're not changing the names of the original variables.

sakchai charoensin
sakchai charoensin
Courses Plus Student 1,112 Points

I have done to start but it still won't work. Anyway, I try as the suggestion above from Piotr Manczak. It works now, thanks for your kindly attention.