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 A Closer Look At Loop Conditions

Rebecca Broughton
Rebecca Broughton
3,876 Points

having issues with while loop

I can't figure out what's wrong- keeps coming up with a parse error or says that code is taking to long to run...

app.js
function secret() = prompt("What is the secret password?");
while(secret.toUppercase!=="sesame") {
  document.write("wrong");
  secret()
}
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="app.js"></script>
</body>
</html>

2 Answers

The challenges are often very particular about the answers you give. But the issue I see in the code you provided is that you created a function called secret when the challenge only asked that you create a while loop that asks the question again if the answer is not sesame. view below

var secret; //FIRST YOU HAVE TO DECLARE SECRET
while(secret !== "sesame"){  //WHILE ANSWER IS NOT SECRET
  secret = prompt("What is the secret password?"); //ASK AGAIN
}
//IF ANSWER = SESAME
document.write("You know the secret password. Welcome."); //WRITE IT TO THE DOCUMENT

I hope this helps.

There are a couple of issues going on that will give you a parse error.

  1. you can write this as a function, but that is not what the challenge wants you to do. Challenges are very fickle as to what they want. Your function does not have an open and closing brackets. "{}" So the while loop is not inside the function. This is where your parse error is coming from.

  2. What the Challenge want you to do is, have an empty string outside of the while loop then iterate through the loop until the input entered is secret, like so:

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

With this code the prompt will continue to loop while until secret equals sesame. I hope this helps.