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 trialziv aviv
2,339 PointsDo....while loop
I Dont understand what i do wrong when i click the check work it says: "You only need to call the prompt() method once, and only inside the loop. Hint: Declare the secret variable before the loop." can someone help me please?
var secret = prompt("What is the secret password?");
do{
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>
3 Answers
SRIMUGAN J
Courses Plus Student 5,345 Pointsvar secret ;
do{
secret = prompt("what is the secret password");
secret = secret + 1;
}
while ( secret === "sesame" ) {
document.write("You know the secret password. Welcome.");
}
1) first declare the variable before the loop, this is coz if you declare inside the variable it would run again and again, hence declare the variable first. 2)what is do while loop is for actually the loop runs at least once even the condition was false, here for you task, prompt the the string "what is the secret password" at first while you enter the wrong password increase the variable by 1 means again you ask the same question , until the given condition become true. The condition which you are giving in while is if the password which is equal to "sesame" the loop ends condition become true write the output as "....".
thats all,
I Hope this helps..
ziv aviv
2,339 PointsSRIMUGAN J thank you so much, its working :)
Justin Radcliffe
18,987 PointsHi Ziv,
You're repeating the string: "What is the secret password?" 3 times; when you actually only need to declare it once - inside the do loop as you currently have it.
-
The do...while syntaxt does not require the curly braces after the while parens:
while ( secret !== "sesame" ) { // ALL OF THIS IS ......... secret = prompt("What is the secret password?"); // <--- HINT: Is this needed anyway? } // NOT VALID "DO .... WHILE" SYNTAX
In the do while statement you're comparing "secret" to "sesame". In your code var secret = "What is the secret password?" Maybe change that to "sesame"?