简体   繁体   English

为什么总是返回true?

[英]Why does it always return true?

Beginner question, sorry if it's not the right place to ask Trying to learn how logic works in JS , can't figure this out 初学者的问题,很抱歉,如果不是在正确的地方提问试图学习JS逻辑工作原理, JS这个问题

if (firstSymbol === "A" || "a" || "D" || "d") {
    if (secondSymbol === "z") {
        alert("It does!"); 
        break;
    }
}

I expect it to say "It does!" 我希望它说“确实如此!” and break in case if the firstSymbol is A, a, D or d AND the secondSymbol is z , but for some reason it says "It does!" 并在firstSymbol是A,a,D或d并且secondSymbol是z的情况下中断,但由于某种原因,它会说“确实如此!” and breaks regardless of what the firstSymbol is and only checks if the secondSymbol is z. 并且不管firstSymbol是什么而中断,并且仅检查secondSymbol是否为z。

Because you're checking whether "a" is true - it is always true: 因为您要检查"a"是否为真-始终为真:

 console.log(!!"a"); 

You should be using includes and AND && in this case: 在这种情况下,您应该使用includes和AND &&

 const firstSymbol = "D"; const secondSymbol = "z"; if (["A", "a", "D", "d"].includes(firstSymbol) && secondSymbol == "z") { console.log("It does!"); } 

 function matchSecondSymbol(firstSymbol, secondSymbol) { // By making FirstSymbol Uppercase, we can remove the other two conditions firstSymbol = firstSymbol.toUpperCase(); if (['A', 'D'].includes(firstSymbol) && secondSymbol === "z") { console.log('it Does'); } else { console.log('it does not'); } } matchSecondSymbol('a', 'z'); matchSecondSymbol('z', 'z'); matchSecondSymbol('a', 'y'); 

In Javascript there's something called truthy and falsy values. 在Javascript中有一种叫做truthyfalsy值。 In summary, is how a value evaluates in a Boolean ( true or false ) context. 总之,是在布尔( truefalse )上下文中值的求值方式。

All values are truthy unless they are defined as falsy (ie, except for false, 0, "", null, undefined, and NaN). 除非将它们定义为虚假,否则所有值都是真实的(即,除了false,0,“”,null,undefined和NaN外)。

In your code, when you wrote: 在您的代码中,编写时:

if (firstSymbol === "A" || "a" || "D" || "d")

You are checking 4 boolean conditions: 您正在检查4个布尔条件:

  1. firstSymbol === "A" - The result will depend on firstSymbol firstSymbol === "A" -结果将取决于firstSymbol
  2. "a" - Will always evaluate to true "a" -始终为true
  3. "D" - Will always evaluate to true "D" -将始终为true
  4. "d" - Will always evaluate to true "d" -始终评估为true

So, since conditions 2, 3 and 4 will always be true , your code will always enter the if statement. 因此,由于条件2、3和4始终为true ,因此您的代码将始终输入if语句。 If even a single one of these would be true the behaviour would be the same. 如果其中只有一个是true则行为将是相同的。

You can rewrite it in some ways: 您可以通过以下方式重写它:

if (firstSymbol === "A" || firstSymbol === "a" || firstSymbol === "D" || firstSymbol === "d")

or 要么

if (["A", "a", "D", "d"].indexOf(firstSymbol) > -1)

or 要么

if (["A", "D"].indexOf(firstSymbol.toUpperCase()) > -1)

Just to add something to the boilerplate, these are some examples to accomplish what you're trying to do, with tons of possibilities. 只是为了增加一些样板,这些是一些示例,可以无数种可能性来完成您要尝试的操作。

Tests covered: 涵盖的测试:

  • using array.includes 使用array.includes
  • using array.indexOf 使用array.indexOf
  • using array.find 使用array.find
  • using array.some 使用array.some

WHY didn't your code work? 为什么您的代码无效?

It didn't work because javascript evaluates the following expression: "A" || "a" || "D" || "d" 它不起作用,因为javascript计算以下表达式: "A" || "a" || "D" || "d" "A" || "a" || "D" || "d" "A" || "a" || "D" || "d" to "A" , because "A" is truthy . "A" || "a" || "D" || "d"改为"A" ,因为“ A”是真实的 If you need to compare with multiple values, either use an array, either write the condition for each of them: firstSymbol === "A" || firstSymbol === "D" ||... 如果需要与多个值进行比较,则可以使用一个数组,也可以为每个值编写条件: firstSymbol === "A" || firstSymbol === "D" ||... firstSymbol === "A" || firstSymbol === "D" ||...

Examples mentioned above: 上面提到的例子:

 /* Original code */ /* if (firstSymbol === "A" || "a" || "D" || "d") {if (secondSymbol === "z") alert("It does!"); break;} */ let firstSymbol = "d", secondSymbol = "z"; // Using .includes if (["A","a","D","d"].includes(firstSymbol) && secondSymbol === "z") console.log('it does, with .includes'); // Cleverer .includes due to the nature of the input. if (["a","d"].includes(firstSymbol.toLowerCase()) && secondSymbol === "z") console.log('it does, with cleverer .includes'); // Using .indexOf if (["A","a","D","d"].indexOf(firstSymbol) > -1 && secondSymbol === "z") console.log('it does, with .indexOf'); // Using .find if (["A","a","D","d"].find(i => i === firstSymbol) && secondSymbol === "z") console.log('it does, with .find'); // Using. some if (["A","a","D","d"].some(i => i === firstSymbol) && secondSymbol === "z") console.log('it does, with .some'); 

Order of Precedence decides 优先顺序决定

In each programming language, symbols are processed in order of their precedence order. 在每种编程语言中,符号都是按照其优先顺序进行处理的。

In Short: 简而言之:

AS the others already explained, your assignment 正如其他人已经解释的那样,您的任务

firstSymbol === "A" || "a" || "D" || "d"

would be processed as 将被处理为

(firstSymbol === "A") || ("a") || ("D") || ("d")

Link to how logical operators are processed: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence 链接到如何处理逻辑运算符: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

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

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