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 trialmicheleg
3,808 PointsI 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
/*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.");
<!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
Treehouse Moderator 145,860 PointsHi 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! :)
Chyno Deluxe
16,936 PointsYour 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.
micheleg
3,808 PointsThank you!! Yes this helps me!
Ted Sumner
Courses Plus Student 17,967 PointsPlease mark best answer for the answer that helped you the most.
micheleg
3,808 PointsAll the answers were wonderful.
micheleg
3,808 Pointsmicheleg
3,808 PointsTHANK 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. :)
Jason Anders
Treehouse Moderator 145,860 PointsJason Anders
Treehouse Moderator 145,860 PointsYou're welcome! :)