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

Can somebody please help me with my code.

Not sure on how to fix this.

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

do {
 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>
Greg Nemes
Greg Nemes
4,261 Points

I think the issue comes from declaring the secret variable outside of the do loop. This does two things. First, it calls the prompt function, which you do not want to happen before the do loop. Second, it means that the users response to the prompt you call inside the do loop is not stored in a variable, and then it cannot be compared in the while condition. I would suggest replacing your fourth line with your first line.

Also, you should not have curly braces after the while ( secret !== "sesame" )

I believe you have to say secret=prompt("what is the secret password?") in the do loop. so that srcret can be assigned a value

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hi Gedeon Bashimbe

You're on the right track and super close.

For the most part, everything is correct and you have all the pieces, it just needs a little fix. First, outside the loop, you should only declare the secret variable... don't assign anything to it here. Remember, there are two parts to using a variable: 1. Declaring it (eg. var secret;) and 2. initializing it or assigning it a value (eg. secret = prompt...)

For this challenge, you'll want to declare the variable outside the loop, but initialize it inside the loop.

I think you'll get it now! Nice work. :)

:dizzy: