Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a `do...while` loop

Do While loop challenge

Hey guys. I'm looking for any help with this challenge. I understand the concept behind Do While loops, but I can't seem to put this code together to pass this challenge. What I have attached probably isn't close to the answer, but i'm going to move on for now. Anything helps, thanks.

let guess;
let secret = sesame;
correctGuess = false;

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

document.write("You know the secret password. Welcome.");
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
<script src="script.js"></script>

2 Answers

Steven Parker
Steven Parker
217,640 Points

You shouldn't need to create any new variables for the new version (like "guess" or "correctGuess").

Hint: Try re-using (but re-ordering) the original lines to create the new version, it might help avoid some of the syntax errors introduced here and keep the code more compact.

3,818 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.