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

Mariel Antonio
PLUS
Mariel Antonio
Courses Plus Student 7,300 Points

The do while loop

Having a hard time understanding the concept of the do while loop. What am I missing?

script.js
var secret = prompt("What is the secret password?");
do {
  secret = prompt("What is the secret password?"); 
  }
} while ( secret !== "sesame" ) 
  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

Mariel, The number of left brackets "{" should equal the number of right brackets "}" and right now you have too many right brackets. Delete the last two right brackets and the code should function better.

Kristopher Van Sant
PLUS
Kristopher Van Sant
Courses Plus Student 18,830 Points

HI! If it helps to understand do...while loops, check out some external documentation, like the Mozilla Developer Network https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while

"The do...while statement creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once."

do {
   statement
}
while (condition);

So for this challenge you could think of it as....

do {
 do the prompt for "What is the secret password"
} while (while their answer for the password is still wrong );

A couple things to note about what you've written so far...first you have the prompt twice, you only need it once inside the do..while loop(the statement). You also want to move your while up behind the closing bracket for the do statement.

Hope this helps, without giving away the exact answer :)

Mariel Antonio
Mariel Antonio
Courses Plus Student 7,300 Points

Thank you very much! I solved it. Sheeesh. Hahaha

Kristopher Van Sant
Kristopher Van Sant
Courses Plus Student 18,830 Points

Hooray!! Haha, it always feels good when you finally solve something. :)