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

You should declare the `secret` variable before the loop. Otherwise, you re-create that variable each time through loop

What this thing does is to burn time while making no sense; I shouldn't have to declare again the variable secret ( I commented it out at the top before the "do" the variable secret and added it as the last expression ( before the loop) inside the if statement to appease the error I'm reporting). The loop is false if the value of secret is reset to what the user input and i if the user did not enter sesame;

I also tried this on jsbin.com and jsfiddle,net and in both cases you go through the loop at lease once: prompting the user twice in total!

If there wasn't a deliberate attempt to burn time and to suck up monthly recurring, Why wouldn't this platform supply one with the correct answer instead of waiting for someone to answer. I dont have long in the world of treehouse. I will use my 30 days and leave. It's spam designed for one intent only: to suck money!

script.js
// var secret;

do {

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

}while ( secret !== "sesame" ); {
  secret = prompt("What is the secret password?");    
}
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

3 Answers

Steven Parker
Steven Parker
231,269 Points

It actually gave you a pretty good hint. You should un-comment that declaration and use it.

But you also have a good deal of unneeded code. For one thing, there's no need to prompt again after the loop. When a user puts in the right word, they should definitely not see another prompt. Then, if you move the "Welcome" message outside of the loop, you don't need that "if" statement or any of the rest of its content at all. After cleaning these things up, you will pass the challenge.

I guess not all teaching techniques work well for everyone, but I personally have found the course structure here very effective and enjoyable.

But be careful about testing challenges in external environments. Each challenge has specific criteria beyond just making code run. If you have misunderstood the challenge, it's also very likely that you will misinterpret the results.

Steven Parker
Steven Parker
231,269 Points

After applying my suggestions, the code would look like this:

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

I dont know what you are talking about: no need to prompt again after the loop?? there is one prompt in the while loop. Witjhout it you defeat the entire purpose of the exercise: to keep asking for the proper answer!

I dont think you understand the components of the do/while you keep referring to the do section as the loop! nothing you say makes any sense if I took out the welcome message and if statement from the do section then if the user guessed properly they'd never reach that decision or welcome--you cant place in the while section, as the entire purpose of the excercise is to avoid the while loop while satisfying the do and getting the welcome.

You may paste your code here and I'd love to see it work, but I dont see how!

Steven Parker
Steven Parker
231,269 Points

Yes, there is one prompt in the loop, I was talking about the second one after the loop.

Perhaps my observations will make more sense as comments in your code:

// var secret;    <-- un-comment that declaration and use it

do {

  secret = prompt("What is the secret password?");
                  // this "if" statement and the rest of its content is not needed
  if(secret === "sesame"){
                  // move the "Welcome" message outside of the loop
  document.write("You know the secret password. Welcome.")
  var secret = 'sesame';
}

}while ( secret !== "sesame" ); {
                  // there's no need to prompt again after the loop
  secret = prompt("What is the secret password?");
}

See the comment I added to my answer for the final result.

perhaps showing me your workable code will be an improvement. I dont agree with your comments

Steven Parker
Steven Parker
231,269 Points

I added the code as a comment to my answer. That should clear up any confusion.