繁体   English   中英

我正在尝试用 javascript 编写一个 while 循环。 我的 console.log 没有打印请求的消息

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

您的代码正在将 while 循环内的变量设置为提示的输出,这就是循环无法访问它的原因。

为了实现你的目标,你需要这样的东西:

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

基本上,代码进入一个无限循环,要求用户在继续代码之前输入y ,在这种情况下,将消息记录到控制台。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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