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

This is the code we used in the last code challenge. After learning about do...while loops, don't you think this would w

it keeps saying code is taking too long to run

script.js
var secret = prompt("What is the secret password?");
while ( secret !== "sesame" ) {
  secret = prompt("What is the secret password?");    
}
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>

3 Answers

Hi.

You have to change from while loop to do-while loop like this:

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

That what the challenge is asking you to do.

why dont you include the prompt when first naming the variable as in the previous challenge asking u to do the same task utilizing while loops?

@jv5 If you include the prompt before the do-while loop then the prompt will be displayed only one time.

The structure of the loop is created in a way so that if the user writes the wrong answer he/she is presented with the message again - so that he/she knows that the answer is wrong in a way.

If the answer is wrong and the prompt runs only one time - well the user will be thinking whats going on in a way...

You have to structure your code so that the UI is friendly and informative.

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Your question is cut so I can only answer about "it keeps saying code is taking too long to run" part.

In short, the code you posted should not produce taking too long to run error. What it means is that your code has entered an indefinite loop which runs forever. Are you sure you posted the right code? not the default code that has been set up for quiz?

let secret = ''; do { secret = prompt("What is the secret password?"); } while(secret !== 'sesame') { alert("You know the secret password. Welcome!"); } this worked for me ...