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

need help guys

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>

var secret; do { secret = prompt("What is the secret password?"); }

while ( secret !== "sesame" ){ document.write("You know the secret password. Welcome."); }

Simon, this was my correct response to the task, have a nice day.

20 Answers

rydavim
rydavim
18,814 Points

A do-while loop is best used when you want to always run the code block at least once, before evaluating a condition.

var secret; // declare the secret variable so you can use it inside the loop
do { // always do the following code block at least once
  secret = prompt("what is the secret password?"); // execute the code inside the block...
} while (secret !== "sesame"); // and THEN evaluate if secret is equal to 'sesame' - if not, run the block again until it is.
document.write("You know the secret password. Welcome."); // once secret = sesame, write to the document

I appreciate that you took the time to add comments with more information!

Jacques Simon
Jacques Simon
6,594 Points

thank you dear sir. really appreciate the help on this one. This segment has been kicking my butt :-(

thanks so much!

Nicholas Wallen
Nicholas Wallen
12,278 Points

So is the secret password Sesame? LOL

Wouldnt !== mean not equal to?

sfrocco
sfrocco
20,298 Points

That's a really great explanation. Thank you for taking the time!

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

you need to add the action you want for do

Rich Braymiller
Rich Braymiller
7,119 Points

I am having a TOUGH time grasping the do while...like completely lost...

Steef Vendy
Steef Vendy
9,436 Points

You are not the only one... my mind is blown away.

Alexander Ayala
Alexander Ayala
4,023 Points

Let's explain it like this,

you have this code:

var x = 1; do { console.log("#" + x); x += 2; } while ( x <= 15);

THE OUTPUT WOULD BE "#1" "#3" "#5" "#7" "#9" "#11" "#13" "#15"

So basically, the DO is to log this '#' with a number (x) by adding 2 to it the WHILE is to keep doing it until it reaches 15 or greater than

Shaun Wong
Shaun Wong
14,489 Points

Alternative Working Answer

var secret 
var correctPass = false;

do { secret = prompt("What is the secret password?");

    if ( secret === "sesame" ) { 
        document.write("You know the secret password. Welcome."); 
        correctPass = true; 
        } 

    } while (correctPass = false)
Sean Adamson
Sean Adamson
6,682 Points

Why would you use this, less lines of code the better ;)

Shaun Wong
Shaun Wong
14,489 Points

At the time i was learning JS, just like everyone else in the course. I agree less is better.

The reason at the time was that i wanted to store the boolean variable so i could use it later.

So i wrote it differently to test what is possible with js... since i was making a video game at the time I was storing stuff like isPlayerDead? or isMonsterDead?

I just posted my alternative working answer.

try not to think to much about it. Loops are simple the logic that allows them to run can some times be cumbersome. As long as there conditions are true they will run but once there conditions are false they stop. "Do While" just gets a head start with one iteration of the loop before checking if the condition is true or not.

var secret; do { secret = prompt("What is the secret password?");
}

while ( secret !== "sesame" ){ document.write("You know the secret password. Welcome."); }

Excellent response Rafael, it worked perfect for me, thanks and have a wonderful day.

var secret; do { secret = prompt("What is the secret password?"); }

while ( secret !== "sesame" ){ document.write("You know the secret password. Welcome."); }

In case the answers above are obsolete. This code will work.

var secret;

do {

secret = prompt("What is the secret password?");

} while ( secret !=="sesame" ) {

}

document.write(You know the secret password. Welcome.");

Abhinav Aggarwal
Abhinav Aggarwal
3,103 Points

That was an easy question..

just make sure you declared the variable on the top without any value and giving that value in do while statment...;

Okay I will explain what I understand about the loop. If I am wrong someone please correct me. This kind of loop will run endlessly if not stopped. Also if it is not stopped this will cause severe problems in the browser. The "do" part of the loop seems to be the process that you want to run(i.e. if you want to prompt your user for information). The "while" part of the loop seems to be where you write a rule that causes the loop to stop running once the task that you have set out to accomplish has been accomplished. In this case once the correct password has been entered this will stop the loop from running.

Matt Coston
Matt Coston
18,425 Points

I like to look at it in a simple: DO { THIS CODE} WHILE ( a === a )

//rest of the program

You're partially correct...the do while loop will keep on running until the user types in the correct password or exits out of the browser. The "do" part runs ONCE and it will keep on running if user types in the wrong password. In other words, no matter what situation, the do part is run ONCE. This particular problem is not an endless loop, so it wouldn't crash the browser.

Ole Jørgen Næss
Ole Jørgen Næss
3,324 Points

Thank you so much. I was really close.

I think it would be best if someone would explain what exactly each kind of loop is used for and why its used that way. Ive done a brief search on google and couldn't find anything substantial that explains.

marcus smith
marcus smith
2,576 Points

This is going to cost me thousands of dollars in psychiatric therapy. I HAVE TRIED everything. There si some sort of magic answer and I am no where near close.

Rich Braymiller
Rich Braymiller
7,119 Points

haha yeah eventually you'll get it is what I've been told...I feel like nothing I do or no matter how hard I try I'll be good enough to get a job but I keep moving forward...here is a good article that helps me stay focused...http://www.vikingcodeschool.com/posts/why-learning-to-code-is-so-damn-hard

Oleg Kushnir
Oleg Kushnir
3,349 Points

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

after ours i've got the answer if it 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.");

Ah, this challenge gave me so much trouble and it's so simple. Having a difficult time grasping this. Ergh.

Ruan Davis
Ruan Davis
7,344 Points

Remember, the do.........while loop will execute the code in the "do{...}" section while the condition stated in the "while()" part remains true.

in this case a pop up should appear until you have entered the correct password.

this is my solution,

var secret;

do { secret = prompt("What is the secret password?"); }**the code will perform the prompt command********

while ( secret !== "sesame" ) *****while the condition is met, in this case the condition is that the password provided is not equal to "sesame". therefore any other password you enter that is not "sesame" will trigger the "do" loop to run over and over.***********************

document.write("You know the secret password. Welcome.");

Gertrude Dawson
Gertrude Dawson
5,371 Points

I got my solution to work perfectly in my Chrome console but received an error from the quiz. When I fiddled my coding it would only provide the prompt and went into a loop where it would accept the "sesame" input and keep offering up the same prompt and when I fiddled with my code further it would keep giving me an error message in the Chrome console and would not run at all.

cornelis Etta
seal-mask
.a{fill-rule:evenodd;}techdegree
cornelis Etta
Front End Web Development Techdegree Student 7,093 Points

var secret;

do { secret = prompt("What is the secret password?"); }

while ( secret !== "sesame" ){ document.write("You know the secret password. Welcome."); } The do statement runs first and if the prompt message sesame was typed at the first attempt, You know the secret password. Welcome. is printed out else the whole statement runs again from the do part, until sesame is typed in. The code doesn't run from the while statement after after the first do is ran. In other words do this while secret is not equal to sesame repeat do until secret is sesame then stop and print You know the secret password. Welcome.