gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:21:53 25 4
gpt4 key购买 nike

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,在本例中,会将消息记录到控制台。

关于javascript - 我正在尝试用 JavaScript 编写一个 while 循环。我的 console.log 不打印请求的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59342728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com