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 trialAmir Abaza
12,713 PointsHelp, please.
I would really appreciate if someone helps me with this.
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>
2 Answers
Liam Clarke
19,938 PointsTo convert the code to a do while loop, there are a few changes that will be made
remove the initial value set on the var secrets, this is because a do while loop is run once before the while condition is checked. If we did not remove it, then the prompt will be shown twice.
follow the syntax of the do while loop, its kind of like a while loop upside down when you compare between them
var secret;
do {
secret = prompt("What is the secret password?");
} while ( secret !== "sesame" );
document.write("You know the secret password. Welcome.");
Rahul Saini
4,611 PointsIt asked you to convert while into do while. The main difference between them is that While : Runs at least once and then check the condition, whereas while checks the condition first and then runs the code.
//The code is:
var secret = "";
do {
secret = prompt("What is the secret password?");
}while ( secret !== "sesame" ) ;
document.write("You know the secret password. Welcome.");
Amir Abaza
12,713 PointsAmir Abaza
12,713 PointsThanks!