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 Create a `do...while` loop

Syntax error

I am getting a syntax error somewhere. Im learning from scratch, so I have a lot still to go. Im having trouble for some reason grasping somethings, and apparently, this is still one of them. Can't tell why I am having trouble here.

script.js
var secret = prompt("What is the secret password?");


do {
  return secret
} while ( secret !== "sesame" ) 

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="script.js"></script>
</body>
</html>

2 Answers

Josephine Whynot
seal-mask
.a{fill-rule:evenodd;}techdegree
Josephine Whynot
Full Stack JavaScript Techdegree Student 55 Points

Hey! I'm a bit new to this also, but I've decided to check out the forums and look at some problems to test myself and help others at the same time. Hopefully this makes sense:

There's a syntax error because you're using "return" outside of a function. But you actually don't need either (a function or return statement).

You want to update the secret variable when the do...while loop runs. So for one, the secret variable doesn't actually need a value at the start, it can just be:

var secret

Then you get the do...while loop to update the secret variable to prompt("What is the secret password?") while "sesame" isn't the value of secret.

var secret

do {
  secret = prompt("What is the secret password?");
} while ( secret !== "sesame" )

and then at the end there should still be

document.write("You know the secret password. Welcome.");

Sorry if this isn't worded very well! I'm not used to writing out explanations on these things but I hope it helped a bit (:

I did not know that I could not use a "return" outside of a function! Unless on what i have learned as of now. Your explanation was great, I really appreciate the time to help me out here. I guess I'm a slower learner and need a bit more time in general than normal. I wouldn't have thought about leaving the var secret; blank and then updating it within the loop.

Josephine Whynot
seal-mask
.a{fill-rule:evenodd;}techdegree
Josephine Whynot
Full Stack JavaScript Techdegree Student 55 Points

No problem! I'm glad I could help! Learning to code can be difficult and time-consuming. I wouldn't assume you're a slow learner, but regardless, you're definitely not alone.