gpt4 book ai didi

javascript - 不要对特定输入执行某些操作

转载 作者:行者123 更新时间:2023-12-02 22:25:11 25 4
gpt4 key购买 nike

input = window.prompt()
x = true

if (input.includes("stop working")) {
if (x = true) {
console.log("not working")
}
x = false
}

if (x = true) {
console.log("working")
}

理论上,当我输入stop working时进入窗口提示,它不应该记录 working ,但是当我输入stop working时它记录not working然后working即使xfalse它应该只记录 working当它是真的时。

最佳答案

问题就在这里if(x=true)您将默认参数传递给x,因此它始终为true,在这种情况下,正确的是:

input = window.prompt()
x = true

if(input.includes("stop working")){
if(x == true){
console.log("not working")
}
x=false
}

if(x){ // here
console.log("working")
}

如果您的目的是比较变量,则必须使用=====,如下所示:

input = window.prompt()
x = true

if(input.includes("stop working")){
if(x == true){
console.log("not working")
}
x=false
}

if(x == true){ // here
console.log("working")
}

但是,在这种情况下,您的变量是 bool 值,因此您不需要在 if 中进行此比较。

如果您想要更优化的代码,您可以这样做:

input = window.prompt()

if(input.includes("stop working")){
console.log("not working")
} else {
console.log("working")
}

关于javascript - 不要对特定输入执行某些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59096545/

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