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

Ali Sh
Ali Sh
2,026 Points

I can't solve this 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 work better in the do...while style? Re-write the code to use a do...while loop.

script.js
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.");
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

3 Answers

Lucian Rotari
Lucian Rotari
12,007 Points

My suggestion would be to rewatch this video

Hi Ali. In this challenge you are to change the loop from while to Do. Remember the concept DRY - Do Not Repeat Yourself. if you check on the code there is a repetition where you are asking What is the secret password? hence the need to adjust the loop to Do. While puts the test condition at the top of the loop as a result you can not ask the question once you are forced to put another question outside the loop like it shows in the challenge. Do...loop puts the test condition at the bottom so the statements within the loop are executed at least once hence Do..loop is said to be better in this case because it gives room to ask the question at least once. Therefore in using the do loop we need to remove the question outside the loop and we remain with one question which is inside the loop. So we ask the question and then then check check if it pasess the test. The code will then look like below:

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

I got the same and correct result even If I put document.write inside do code block. Is it acceptable ?

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

I think this example would write every time the loop runs. The success message should be displayed once, only if the user knows the password