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

I'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.");
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

Hi 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.");

Elian,

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!

Hey 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.

Thank you for your help, Elian!

White Moses
White Moses
3,589 Points

Your 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.");