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 trialJules Al
1,096 PointsI'm trying to complete Challenge task 1 of 1 with while loops. My current code keeps returning a syntax/parse error.
My code is:
var secret = prompt("What is the secret password?");
var guess;
while(guess !== "sesame"){
return 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>
2 Answers
elk6
22,916 PointsHi Jullian,
The secret var itself is the answer to the prompt. Whatever the user enters in the prompt will be the "secret" var. So there is no need for the "guess" var. Try it like this:
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.");
White Moses
3,589 PointsYour progam needs to ask a question until the right answer will be entered. So you need while loop and question inside.
var secret;
while (secret !== 'sesame') {
secret = prompt("What is the secret password?");
}
document.write("You know the secret password. Welcome.");
nik
8,925 Pointsnik
8,925 PointsElian,
So inside the while loop, couldn't we call the variable "secret" again instead of typing it all out? or would a function needed to be created in order for that to work? Just curious to save typing.
Thanks Elian you are awesome!
elk6
22,916 Pointselk6
22,916 PointsHey Nick,
Unfortunately not. In order to call the var without setting it you would need to set a return statement, but that would mean that the loop will stop ( return always stops the execution of a function ). So, as far as i know, you would have write it all out again inside the loop.
Jules Al
1,096 PointsJules Al
1,096 PointsThank you for your help, Elian!