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

Jesse Fister
Jesse Fister
11,968 Points

create a do while loop

it is asking me to create a do while loop... this appears to be correct to me but it is saying my code is taking too long to run?

var secret = prompt("What is the secret password?"); var guess = prompt(secret); var correctGuess = false; var guessCount = 0;

do { guess = prompt("What is the secret password?"); guessCount += 1; if (parseInt(guess) === "sesame") { correctGuess = true; } } while ( ! correctGuess )

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

script.js
var secret = prompt("What is the secret password?");
var guess = prompt(secret);
var correctGuess = false;
var guessCount = 0;

do {
  guess = prompt("What is the secret password?");
  guessCount += 1;
  if (parseInt(guess) === "sesame") {
    correctGuess = true; 
  }
}  while ( ! correctGuess ) 


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


//var guess = prompt(secret);

/*while (guess !== "sesame") {
  prompt(secret);
  secret = prompt();
}*/
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

Hugo Paz
Hugo Paz
15,622 Points

Hi Jesse,

You dont need auxiliary variables nor do you need an if. You can use the while to test the condition

var secret;
do {
  secret = prompt("What is the secret password?");
} while ( secret !== 'sesame' );
document.write("You know the secret password. Welcome.");
Robert Mews
Robert Mews
11,540 Points

Hey Hugo,

I was trouble shooting the Javascript do while loop challenge when I saw this. Don't we need a conditional in the do part? What happens if the prompt is answered correctly on first attempt?

Hugo Paz
Hugo Paz
15,622 Points

Hi Robert,

The advantage of the do...while loop is that it always runs at least one time.

So if we change the code above to this:

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

Even though the variable secret already has the correct value, the do..while loop will still run. And the prompt value will override the current one.

Regarding the prompt being answered right the first time. The prompt is inside the loop, so for the prompt to be called, the loop needs to run at least once (assuming you input the correct value the first time). The while part checks the condition. If its true the loop terminates.

Zherui Li
Zherui Li
993 Points

thank you Hugo, your explanation very detail and helpful!

Stephen Acheson
Stephen Acheson
7,412 Points

Thanks for this. Was a little stuck on this and knew it was something simple I was missing.