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 trialBularca Andrei
2,408 PointsWhy is the syntax error ?
.
var secret = prompt("What is the secret password?");
while(secret === 'sesame')
{document.write("You know the secret password. Welcome.");}
else
{prompt("what is the secret password?");}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
3 Answers
Matthew Rigdon
8,223 PointsWell your code doesn't do what you intended it to. While loops are used to continually cycle over a segment of code. Matthew Panton shows how you could use your while loop to continue asking the question about the secret password.
Right now your code will ask the question, and if it correct, it will keep printing out that you are correct, since you told it to use a while loop. The code will continue to run until you end it. If the password is wrong, it will print out that the password is wrong.
I don't see any syntax errors, other than maybe your spacing inside of the HTML code. I believe it is flagging the question as erroneous due to a continuous while loop.
Matthew Panton
15,228 PointsWith regards to Matthew Rigdon's answer, there is a syntax error in the original code because you cannot use an "else" statement without an "if" statement before it.
Bularca Andrei
2,408 PointsThe problem was the else , thank you both guys !
Matthew Panton
15,228 PointsMatthew Panton
15,228 PointsHi, a while statement is used for loops and cannot be followed by an else clause. I'm not sure what the aim of this code challenge is but this might be the thing to do