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

Pratik Rai
Pratik Rai
6,856 Points

what is wrong with this. frustrating me.

I cannot get it through. any explanation would be appreciated.

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>

4 Answers

Oh I'm sorry i misunderstood the question. The problem with your code is that you have the var in two places with the same value. you should create the empty variable as a global scope var and then give it a value inside the do loop.

Here is how you would write the code.

var secret;

do {
  secret = prompt("What is the secret password?");
}
while ( secret !== "sesame" );
document.write("You know the secret password. Welcome.");
Pratik Rai
Pratik Rai
6,856 Points

thanks you. I understand what you say but this code doesn't pass the test.

Pratik Rai
Pratik Rai
6,856 Points

hmmm this makes sense. thank you. btw I don't like these javascript tutorials as much as <a href="http://channel9.msdn.com/Series/Javascript-Fundamentals-Development-for-Absolute-Beginners/Series-Introduction-01">Bob tabor's tutorials</a>.

haha. yeah its rough but if you haven't tried codecademy's tutorials i recommend it. Their JavaScript course was easier to understand.

Pratik Rai
Pratik Rai
6,856 Points

yes I already finished it and the bob tabor's as well. but after starting with dave makes me feel myself like an infant. and the worst part I am just waiting this javascript's end so that I can only proceed to the next part.

haha. hang in there bud.

Pratik Rai
Pratik Rai
6,856 Points

thank you Colin. Chyno already helped me out.