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

Ali Amirazizi
Ali Amirazizi
16,087 Points

What's wrong with my code?

The code is stuck in an infinite prompt loop, and I can't figure out how to pass the challenge.

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

do {
prompt(secret); 
if (guess === "sesame") {
  correct = true;
}

} while (!correct);

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

Chris Shaw
Chris Shaw
26,676 Points

Hi Ali,

You seem to be confused with the operation of do..while loops and what the challenge is asking for, some things to note.

  1. You have 3 separate prompt declarations, the challenge is only expecting one
  2. You have created a new variable correct which is an unneeded condition in the while declaration
  3. The only code you need in the do execution is the call to prompt

See the below

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

Happy coding!