Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a `do...while` loop

Hyeri Chung
Hyeri Chung
2,178 Points

I can't understand what the question wants me to do anymore.

I think this code works for the asked query but I can't meet the expected answer to this question. will be appreciated if you can have a look for my code. what should I change for the answer? is it necessary to follow the question answer's instruction?

var password = 'sesame' var correctanswer = false;

do { var secret = prompt('what is the secret password?'); if ( secret === password ) { correctanswer = true; document.write('you guessed the right password. :p'); } } while ( ! correctanswer )

script.js
var password = 'sesame'
var correctanswer = false;

do {
 var secret = prompt('what is the secret password?');
 if ( secret === password ) {
    correctanswer = true;
   document.write('you guessed the right password. :p');
     }
} while ( ! correctanswer )
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

Steven Parker
Steven Parker
217,626 Points

For best results, always follow the challenge instructions and do exactly what they ask for and nothing extra.

Here's a few hints for this one:

  • you only need to call the "prompt" method once, and only inside the loop
  • declare (but don't assign) the "secret" variable before the loop
  • the inner body of the loop should stay the same
  • the line after the loop should stay the same