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

Do... while challenge, confused by the suggestion about only running the prompt() method inside the loop. Aren't I?

var secret = prompt("what is the secret password?");

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

document.write(......)

The 'Bummer!' message is telling me that I should only run the prompt method once, within the loop, and I am so confused as to how I'm not doing that. And I have changed none of the code, only re-organized it into the do while. ??? Please don't say syntax error. Or please do? Dang.

script.js
var secret = prompt("What is the secret password?");


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

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>

1 Answer

Hey Kathryn,

Try writing it like this:

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

As the error message indicates, you should only run the prompt method once. If you look at your code, you are prompting the user twice for input (When declaring the 'secret' variable and also within your do while loop). With that being said, it's important to remember that do while loops always run at least once.

Best,

Jacob

Thank you! That was what they wanted.

Follow up question though for my own understanding...

Wouldn't my code still produce the same result in terms of the function of the loop? I understand that I am prompting them once unnecessarily, and even if they entered 'sesame' the first time, I would still just prompt them again... I guess I just need to feel like I'm understanding the content after spending 30 minutes on this challenge. Charity backpat? I get this right?

Hey Kathryn,

There is one key difference between a while loop and a do-while loop.

Any code placed within a while loop will only run if the boolean condition of that loop is met.

Example:

var example = prompt("Enter a positive integer, please.");
example = parseInt(example);
while(isNaN(example) || example < 0) {
    example = prompt("I'm sorry, please enter a positive integer.");
}

This while loop will only run if the user enters anything other than a number or a number less than zero.

However, unlike while loops, do-while loops will always run at least once.

var exampleTwo;
do {
   exampleTwo = prompt("Please enter an integer.");
   exampleTwo = parseInt(exampleTwo);
} while(isNaN(exampleTwo) || exampleTwo < 0);

This do-while loop will basically do the same as the while loop. As mentioned, it will always run at least once but will only continue to run if the user enters anything other than a number or a number less than zero. You could most likely re-write the code to be more efficient but this is just to get the point across between the difference of while and do-while loops.

In short, while loops will only run if the boolean condition is true. Do-while loops will always run once but only continue to run if the boolean condition is true thereafter.

Best,

Jacob