javascript - 我正在尝试在 javascript 中编写一个 while 循环。我的 console.log 不打印请求的消息
问题描述
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
在继续代码之前输入,在这种情况下,将消息记录到控制台。
推荐阅读
- java - Selenium ChromeDriver 允许 cookie(默认配置文件)
- android - 在android studio中创建新模块时不生成代码
- javascript - Selenium 正在返回 PageSource 的旧状态,并且在 Javascript 执行后没有更新
- javascript - 2020 年是否可以在外部域 iframe 中设置样式?
- ios - 如何处理本机模式取消/错误事件(cordova-plugin-purchase)?
- python - 在 Pandas 中连接整行
- multithreading - 是否可以使用多线程与 keras 进行超参数调整?
- powershell - Powershell import-csv 一个没有标题的 csv ~1200 列
- mysql - mysql。验证已还原数据库的完整性
- haskell - 交换函数的意外属性?