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

Alexandros S.
Alexandros S.
8,261 Points

"Create a do while loop challenge" problem

So i'm having a problem with this challenge where i have to make a "do while" function that asks for a password. This is the code that i tried on my browser and works properly but i still get an error saying that i only need to call the prompt() method once.

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>

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Indeed, you do only need it call it once. You're calling it twice. You call it when initializing the variable secret and then again in the do while loop which is always guaranteed to run at least once. The trick here is not to initialize secret. all you need to do is change the top line to this:

var secret;

You could also do:

var secret = "";
Alexandros S.
Alexandros S.
8,261 Points

Yea it worked! Thanks a lot, this challenge was breaking my brain.