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

Hudson Lanier
Hudson Lanier
6,329 Points

My code works, but I'm getting an incorrect result.

Hi, I checked my code by running it in the console, and it worked. I was stuck on this for several days, but decided to move on. I'm not entirely sure why I'm not getting a correct result here. Please help.

var secret = prompt("What is the secret password?"); var correctPassword = false;

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

script.js
var secret = prompt("What is the secret password?");
var correctPassword = false;

 do{
  prompt(secret) ; 
   if(secret = "sesame"){
     correctPassword = true
      }
} while ( ! correctPassword )
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>
JEFF CHAI
JEFF CHAI
7,564 Points

Your code is work, but have a mistake there.

var secret = prompt("What is the secret password?");  <!-- here will prompt to ask user to key in secret password -->
var correctPassword = false;

 do{
  prompt(secret) ;    <!-- In this line, you request user to re-key in secret password even then user is type the correct password "sesame" in above, it is because do...while loop is run through statement then only check the condition -->
   if(secret = "sesame"){
     correctPassword = true
      }
} while ( ! correctPassword )
document.write("You know the secret password. Welcome.");

3 Answers

JEFF CHAI
JEFF CHAI
7,564 Points
var secret;
do  {
   secret = prompt("What is the secret password?");    
}while( secret !== "sesame" )
document.write("You know the secret password. Welcome.");

You starting need to declare secret as variable. Once you set it into a variable, continue get in do...while loop. In this do..while loop, it will keep on asking the user to key in the secret password when the password is not match up the condition 'secret !=="sesame" ' When the condition is true, it will break out from the loop and continue your following query.

Paul Walker
Paul Walker
28,904 Points

var secret = 'sesame';

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

Paul Walker
Paul Walker
28,904 Points

var secret = 'sesame';

do {
  prompt("What is the secret password?");
           }
    while ( secret !== "sesame" ) {
             secret = "What is the secret password?";    
        }

document.write("You know the secret password. Welcome.");