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 trialAlex Flores
7,864 PointsWhy won't this work? do while loops JS
var secret = prompt("what is the secret password");
do { alert(secret); if (secret === "sesame") { secret = true; } }
First off, i got the right answer, but I wanted to know why this won't work. I don't know why I have to add the prompt inside the dowhile loop.
Also, can someone tell me how to copy/paste code?
1 Answer
Dan Johnson
9,385 PointsI think you want something more along these lines.
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
Alex Flores
7,864 PointsThank you for answering both of my questions.. This is what I was looking for!
Dan Johnson
9,385 PointsDan Johnson
9,385 PointsIf you put your code inside three tick marks (the unshifted tilde key) it should format correctly.
// I am code
For best results put the ``` on it's own line.
tick tick tick
pasted code
tick tick tick
The "Markdown Cheatsheet" has more info on this I believe.