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

can someone help

what am i doing wrong?

script.js
var secret = prompt("What is the secret password?");
do{
secret = prompt("What is the secret password?");
}
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>

3 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Devanshi,

You pretty much have it correct, except you opening declaration on the variable.

Outside of the loop, you want to declare the variable, but leave it blank, because inside of the loop, you are assigning a value to the already declared variable. Either way, the variable get overwritten, but you want to go into a loop with an empty variable, so as not to 'confuse' the loop.

So just change your opening var declaration and you're good to go.

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

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

Keep Coding! :)

Thanks Jason!! do you think I can be a coder?

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

I think if it's something you are passionate about and enjoy... absolutely! I only started learning code about 8 months ago. I realized I really enjoyed it and with many hours of learning and practicing... I'm hoping (soon) to make it my new career.

Just remember, lots of learning... lots of questions... and lots of practice.

What I found is really helpful in learning - before I start lessons for the day, I read through the forum. I answer the questions that need answering, but I find I learn much by reading questions that have already been answered. They either explain something I didn't really understand, or they show me a different (sometimes) better way to do something.

Good Luck in your endeavors and KEEP Coding! :)

Thank you so much jason!! you started just 8 months ago?? wow!! It just that I enjoy coding. I am passionate about it, I dont know!! I just know one thing that I enjoy it. its wonderful that how these tidbits of words create some beautiful and interactive websites and apps, it s just that I am really looking for a means to earn. so these are basically the reasons I am learning coding 1. I just enjoy to do it and 2. If i could make a living out of it

by the way jason!! I just saw your profile, you are a writter. one confession. I want to be a writter as well. the thing is that I enjoy doing it and yess I am even passionate about it