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 trialDwayne Sauls
1,279 PointsIt keeps on repeating "Try again"
I made it strictly equals equalsto check the data in the variable it holds. But still nothing to return.
var secret = prompt("What is the secret password?");
while(secret === 'sesame'){
alert(secret);
}
document.write("You know the secret password. Welcome.");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
4 Answers
Bryan Peters
11,996 PointsNothing inside your loop changes the condition, so if you were to actually run this as written and enter "sesame" when prompted, the while loop would repeat infinitely.
So,
- The prompt needs to be inside the loop, so that the value of secret can change with each iteration.
- The loop should repeat as long as the response is NOT equal to sesame. The goal is to keep repeating the prompt until they give you the correct response.
- You don't need the alert inside the loop
Dwayne Sauls
1,279 PointsBryan Peters its still the same and it didnt work.
Bryan Peters
11,996 Pointsre-post JS code please.
There are a number of ways to do this. Here is another approach:
while(prompt("What is the secret password?") !== 'sesame'){
alert("Sorry, try again");
}
document.write("You know the secret password. Welcome.");
Dwayne Sauls
1,279 PointsBryan Peters Alena Holligan I did try it like you did and I took away the alert as you said
while(var secret = prompt("What is the secret password?") !== 'sesame'){ } document.write("You know the secret password. Welcome.");
Dwayne Sauls
1,279 PointsJennifer Nordell and Liam Walls I just can get this exercise to pass through
Jennifer Nordell
Treehouse TeacherHi there! It looks like you're redeclaring your variable inside your while, which was not the intention. Also, there are no statements inside your while loop to get a new value for secret. Here was my solution:
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.");
Here we ask for the secret password. If it's not equal to "sesame" we start the while loop which asks for the secret password. This continues until the user enters "sesame". Upon the user entering "sesame" the while loop exits and the welcome message is written to the DOM.
Good luck!
Lee Cockcroft
5,147 PointsLee Cockcroft
5,147 PointsHI
try the code below:
I am also a beginner, (so there may be a better alternative)
this below is saying when the answer is incorrect keep asking to try again, as soon as the correct answer is given the loop breaks.
Again, sorry if this doesn't answer your question, as i am a beginner, I try to debug these as it helps me learn.
I hope my answer helps
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JavaScript Loops</title> </head> <body>
<script type="text/javascript"> var secret = prompt("What is the secret password?"); while(secret !== 'sesame'){ var tryagain=prompt("that is incorrect please try again"); if(tryagain==="sesame") { break;
} } document.write("You know the secret password. Welcome.");
</script>