Ad

I Am Trying To Write A While Loop In Javascript. My Console.log Does Not Print The Requested Message

- 1 answer

var ask = prompt('Are we there yet???');

while (ask != 'y') {

  if (ask[0] === 'y') {
    // For some unknown reason to me my solution will not print the message.
    console.log('Yea, we made it!!!');
  } else {
    var ask = prompt('Are we there yet???');
  };

}

Ad

Answer

Your code is setting a variable inside of the while loop to the output of the prompt which is why the loop can't access it.

To achieve your goal, you'll need something like this:

while (prompt('Are we there yet???') !== 'y') {}
console.log('Yea, we made it!!!');

Basically, the code goes in an infinite loop of asking the user to type y before continuing the code and in this case, logging the message to the console.

Ad
source: stackoverflow.com
Ad