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

I'm unsure what exactly goes on line 6?

.

script.js
var = secret
var = answer

do {
    prompt('What is the secret password?');
} while (answer === '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>

1 Answer

Andreas Nyström
Andreas Nyström
8,887 Points

Hi! A few things:

// Don't declare variables like this.
var = secret
var = answer
// Should be like this:
var secret = ''
var answer = ''

// do means do this at least one time. 
do {
// you should probably have the variable here, instead of outside the loop
    prompt('What is the secret password?');
// This says: keep doing the loop if the answer IS 'sesame'.
// What you want is to say: keep doing the loop if the answer IS NOT 'sesame'
} while (answer === 'sesame')  
document.write('You know the secret password. Welcome.');

So in short it should be something like this:

// Run the loop atleast once (do)
do {
  // Make one variable inside the loop instead of outside that will have the value of whatever the user writes in the prompt.
  var secret = prompt('What is the secret password?');
// Keep doing the loop until the value of secret is 'sesame'
} while (secret !== 'sesame')  
document.write('You know the secret password. Welcome.');