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

JavsScript (while and do-while loops)

Hi all,

I just started learning JavaScript, but I am really struggling to understand the concept. I have spend days to understand do-while loop, and still trying everyday to understand the concept.

I have been trying to sole this challenge with no success:

// Display the prompt dialogue while the value assigned to secret is not equal to "sesame" let secret = prompt("What is the secret password?");

do { let secret = prompt("What is the secret password?"); } while ('' ); // This should run after the loop is done executing alert("You know the secret password. Welcome!");

Here is my answer: // Display the prompt dialogue while the value assigned to secret is not equal to "sesame" let secret = prompt("What is the secret password?");

do { console.log(secret); } while('secret' !== 'sesame') // This should run after the loop is done executing alert("You know the secret password. Welcome!");

1 Answer

Hi Lina. In order to check if the user entered the correct password, you can use an if statement in the while loop like this:

While loop

let input;

while (input !== 'sesame') {
  input = prompt('What is the secret password?')
  if (input === 'sesame') {
    console.log('You know the password, welcome.');
    break;
  }
}

Do-while loop

let input;

do {
  input = prompt('What is the secret password?')
  if (input === 'sesame') {
    console.log('You know the password, welcome.');
    break;
  }
} while (input !== 'sesame');