简体   繁体   中英

I am trying to write a while loop in javascript. My console.log does not print the requested message

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM