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

Do while loop error!

I'm trying to complete a challenge but I can't seem to figure it out :(

Basically is asking me to convert a while loop in to a do while loop that will ask the user to enter password every time that is not equal to "sesame".

Here is my code:

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

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

4 Answers

Andrey Misikhin
Andrey Misikhin
16,529 Points

I give you an idea of how it may be. This code works for me, you may declare "secret" outside of while block.

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

document.write("You know the secret password. Welcome.");
Andrey Misikhin
Andrey Misikhin
16,529 Points

Next time use markdown to format your code on the page.

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

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

That did not work because that is converting a var secret every time the loop runs. My code works when I try in Chrome console but probably not the most efficient way to do it, the error I get says that I only need to call prompt() once.

Thank you!