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 Simplify Repetitive Tasks with Loops Create a do...while loop

Add a do...while loop that keeps displaying the prompt dialog until the user types 'sesame'.

script.js
// Display the prompt dialogue while the value assigned to `secret` is not equal to "sesame"
let password = '';
do {
  let secret = prompt("What is the secret password?");
// This should run after the loop is done executing
alert("You know the secret password. Welcome!");
} while ( password !== "sesame" );        

5 Answers

cazepeda
cazepeda
6,099 Points

FIRST, erase everything in the script.js. The instructions on there are confusing as heck! But I understand that we need to use the do - while loop. The loop consisting of the prompt to capture what the user inputs. We must keep showing the prompt until the user enters sesame into the prompt field. Ok so lets do it!

let secret;  // create an empty variable for secret.

do { // start the do statement
    secret = prompt("What is the secret password?"); // assign prompt() to secret and show user the field to enter answer.
  } while (secret !== "sesame"); // check user input if `sesame` entered othewise run do {} statement.

alert("You know the secret password. Welcome!"); // if answer if correct display alert message.

Try this -

// Display the prompt dialogue while the value assigned to `secret` is not equal to "sesame"

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


// This should run after the loop is done executing
alert("You know the secret password. Welcome!");
Steven Parker
Steven Parker
231,172 Points

The variable name for storing the answer should be consistently "secret" (instead of "password").

And you can only declare a variable once, so there should be no "let" before the assignment of the prompt.

Also, the alert function call (and the comment that goes with it) should go outside the loop, after it finishes.

It seems the challenges are always different to what's been exactly taught in the lessons - treehouse is always like that.

Yea, this challenge is a bit out of place in my opinion, took me a while to understand what they were asking, maybe because i'm newer, but once I wrote it out, it made more sense.