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 trialVasko Jocevski
Courses Plus Student 1,040 PointsCan't solve this !!!!
This is the code we used in the last code challenge. After learning about do...while loops, don't you think this would work better in the do...while style? Re-write the code to use a do...while loop. Important: In each task of this code challenge, the code you write should be added to the code from the previous task. Restart Get Help Check Work script.js index.html
1
var secret = prompt("What is the secret password?");
2
while ( secret !== "sesame" ) {
3
secret = prompt("What is the secret password?");
4
}
5
document.write("You know the secret password. Welcome.");
var secret = prompt("What is the secret password?");
while ( secret !== "sesame" ) {
secret = prompt("What is the secret password?");
}
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>
5 Answers
Bradley Martin
2,777 Pointsvar secret;
do {
secret = prompt("What is the secret password?");
}
while ( secret !== "sesame" )
document.write("You know the secret password. Welcome.");
Dave StSomeWhere
19,870 PointsThis challenge really is a good use case to demonstrate a while loop.
It might help to break down the steps before worrying about the code:
- Prompt the visitor for a password.
- While the password is not correct (not sesame) continue prompting for a password (forever) until the correct password is entered.
- When the correct password is entered exit the while loop with a false condition.
- Give the visitor a message that they entered the correct password
- The end
Does that help?
Vasko Jocevski
Courses Plus Student 1,040 Pointsthat's OK, BUT i need to write it DO WHILE loop....
Dave StSomeWhere
19,870 PointsAha - that make more sense if you look at what's duplicated in the while loop (not DRY) could be simplified with a do while. MDN while vs MDN do while
The important thing about a do while is that the code in the do block is always executed at least once before checking the while condition.
So, you would combine steps 1 and 2 into the do block - does that make sense?
Abraham Juliot
47,353 PointsEDIT: No, the do while loop will not "work better". But, it can accomplish the same thing as the while loop -- both work just fine.
Sekoyya Little
Front End Web Development Techdegree Student 7,651 Pointsnone of you make any sense FYI. Nobody answered the question
Abraham Juliot
47,353 PointsSorry... I added an edit. Hope that helps for clarity :)