Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

javascript - 不要对特定输入执行任何操作(Don't do something on specific input)

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")
}

Theoretically, when I enter stop working into the window prompt, it shouldn't log working , but when I enter stop working it logs not working and then working even though x is false and it should only log working when it is true.(从理论上讲,当我在窗口提示中输入“ stop working ”时,它不应记录为working ,但是当我输入“ stop working ,即使xfalse ,它也记录为not working ,然后仍working ,并且仅当它为true时才记录working 。)

  ask by Emusaurus translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is here if(x=true) you're passing a default parameter to x , so it's always be true, the correct in this case, is:(问题出在这里, 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") } If your intention, is to compare variables , you have to use == or === , like this:(如果要比较变量 ,则必须使用===== ,如下所示:) 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") } But , in this case, you variable is a boolean, so you don't need to do this comparison in the if .(但是 ,在这种情况下,您的变量是一个布尔值,因此您不需要在if进行此比较。) And if you want a more otimized code , you can do this:(如果您想要更优化的代码 ,可以执行以下操作:) input = window.prompt() if(input.includes("stop working")){ console.log("not working") } else { console.log("working") }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...