JavaScript

Lina John
Lina John
1,846 Points

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

Byron Injeeli
seal-mask
.a{fill-rule:evenodd;}techdegree
Byron Injeeli
Full Stack JavaScript Techdegree Student 21,001 Points

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');