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

I don't understand why this is wrong.

Hello I'm stuck on this and I don't understand why it's wrong. It seems to be an endless loop because when I run it in the browser it breaks the page and I need to reload. But why? Any help appreciated, thanks- Michele

script.js
/*var secret = prompt("What is the secret password?");
while ( secret !== "sesame" ) {
  secret = prompt("What is the secret password?");    
}
document.write("You know the secret password. Welcome.");*/

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

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

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>

3 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Michele,

There are a few thing wrong with the code you're trying to run:

First, parseInt turns something into an integer, and the answer you are checking against is a string, not a number... so you need to get rid of that.

This is what is causing your infinite loop (and breaking the browser). Because you were converting a string to a number, the while condition was always coming up false and starting the loop over. So, you need to change that to look for a 'true' false response.

Lastly, your first "prompt" is outside the loop. You can eliminate this and just leave the one in the loop. With both, the first one executes, then the second one executes before either on is check against your conditionals.

The corrected code is below for your reference:

var correctGuess = false;

do {
  secret = prompt("What is the secret password?");
  if (secret === "sesame") {
    correctGuess = true;
  }
}
while ( correctGuess == false  )

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

Hope this helps and clears things up for you. Keep Coding! :)

THANK YOUUUU! You're an angel. Right of course! Sometimes when I'm staring at it I just can't see the problem. Have a good one. :)

Your conditional statement inside your do...while loop is parsing a string value into an integer. Remove the parseInt() and your code should work fine. Also, removing the prompt from the initial secret variable will prevent the prompt from popping up twice.

var secret;
var correctGuess = false;

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

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

I hope this helps.

Thank you!! Yes this helps me!

Please mark best answer for the answer that helped you the most.

All the answers were wonderful.