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???');
};
}
您的代码正在将 while 循环内的变量设置为提示的输出,这就是循环无法访问它的原因。
要实现您的目标,您需要这样的东西:
while (prompt('Are we there yet???') !== 'y') {}
console.log('Yea, we made it!!!');
基本上,代码进入一个无限循环,要求用户在继续代码之前键入y
,在这种情况下,将消息记录到控制台。