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

matthew roach
PLUS
matthew roach
Courses Plus Student 1,089 Points

what have I done wrong?

please check code

script.js
var secret = prompt("What is the secret password?");

do {
  secret = prompt("Try again!");  
}
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>

1 Answer

nico dev
nico dev
20,364 Points

Hi matthew roach ,

Hint: Open another tab in your browser, and open Dev Tools. Paste the code and try it. Try to enter the right input ('sesame') the first time around. It will still ask you the question within the do loop. Why? Because the do loop will always execute the condition (between curly brackets) at least once. Meaning: you already declared secret and assigned it the input received the first time, but no matter what it will execute the do loop (ask you again). If that already helps you solve it, go ahead. :)

Otherwise, read the following snippet.

// Your code above analyzed.
var secret = prompt("What is the secret password?"); // Here you declare the secret var and assign it the first input you receive.

do {
  secret = prompt("Try again!");  // Here you ask again the user to enter another input.
}
while ( secret !== "sesame" ) // Here you compare the second input received with the 'sesame' word.
                                     // If equal exit loop, otherwise start again.

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

====================

// A possible solution analyzed:
var secret;           // Here you declare the secret variable, 
                            // but you don't assign it any value,
                            // and even more importantly you still don't ask any input from the user.

do {
  secret = prompt("What is the secret password?");  // Here you ask for first time that the user enter an input.
}
while ( secret !== "sesame" )    // Now you compare the first input with 'sesame'.

Makes sense? Hope it helps you go on now. Keep it up!