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 trialJirka Michalik
11,573 PointsThis 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 :)
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.");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
2 Answers
Jonathan Hamada
4,810 PointsJust curious, Max can you substitute a while statement? I thought for and while statements were used for loops.
Max Karacsony
23,928 PointsHey 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 ;)
Jonathan Hamada
4,810 PointsThanks max
Jirka Michalik
11,573 PointsOh thanks max ... have a nic day :)
Max Karacsony
23,928 PointsMax Karacsony
23,928 PointsYou 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: