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

Martina Reiter
Martina Reiter
17,654 Points

Do ...while code rewrite

He keeps telling me my code took too long to run...

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

1 Answer

Grace Kelly
Grace Kelly
33,990 Points

Hi Martina, you have the right idea but not quite. The challenge is asking you to rewrite code so that it does the exact same thing but using a do while loop. So what we want to do is keep asking the user for the password as long as the password does NOT equal sesame, we can do this in the following way:

var secret; //declare the secret variable
do {
  secret = prompt("What is the secret password?"); //ask for the password
}
while ( secret !== "sesame" ) //keep asking while the secret variable is NOT equal to "sesame"
document.write("You know the secret password. Welcome.");

Hope that helps!!

Martina Reiter
Martina Reiter
17,654 Points

Yes, it worked :) Thank you!! I think i get the concept now