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

Alfredo Takori
Alfredo Takori
1,818 Points

What am I doing wrong?

Can someone tell me what I am doing wrong? I thought that by declaring the var as secret then I could just input secret for the prompt sequence to run each time

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

You are very close!!!

var secret = ""; 
do { secret = prompt("What is the secret password?");
} while ( secret.toUpperCase() !== "SESAME" )
document.write("You know the secret password. Welcome.");
Alfredo Takori
Alfredo Takori
1,818 Points

Sorry but can you explain why you use "" for the variable?

I come from Java programming and when you declare a localized variable (one within a method) you are supposed to set the String to empty quotes if you aren't immediately going to store actual data in there. javascript might let you do it I can't remember, but it's at least habit when I do it. I hope this helps.

I didn't put the declaration inside the do while loop because it would throw an error, and if it didn't it would try to declare the variable each time through the loop which wouldn't be efficient at all; so I had to declare the String variable outside the do while loop with empty quotes and then set it to take input from the user each time through the loop.