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

This is the code we used in the last code challenge. After learning about do...while loops, don't you think this would w

I have trouble with this task ... can you help me ? I still don't see fault in my code. Thank you for your help :)

script.js
var secret = prompt("What is the secret password?");
do {
  if (secret !== "sesame") {
    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>
Max Karacsony
Max Karacsony
23,928 Points

You confused the condition in the while statement: it's still (secret !== sesame) – so you don't need an if statement in the do-block. This should get you through:

var secret;
do {
  secret = prompt("What is the secret password?");
} while ( secret !== "sesame" )
document.write("You know the secret password. Welcome.");

2 Answers

Just curious, Max can you substitute a while statement? I thought for and while statements were used for loops.

Max Karacsony
Max Karacsony
23,928 Points

Hey Jonathan, In this case a replacement wouldn't make much sense. In general I would say that while loops are used for indefinite loops and for loops for finite loops. In this case we can't say how many attempts a user needs to enter the correct password so we use while. If we wanted to log out the items of an array we would use a for loop because we know the length of the array and so the loop would be finite. I hope you get my point ;)

Thanks max

Oh thanks max ... have a nic day :)