简体   繁体   English

为什么这段代码返回 true 而不是 false

[英]why is this code returning true instead of false

here is the code:这是代码:

const str = "asdfcvb";
const result = str.split("").every((e) => {
    return e==='a'||'b'});
console.log(result);

I expect str.split("") returns an array, and every method check if every element in the array equals to 'a' or 'b', which should return false, but it returns true我希望 str.split("") 返回一个数组,并且每个方法都会检查数组中的每个元素是否等于'a'或'b',这应该返回false,但它返回true

Because e==='a'||'b' means ((e==='a')||'b')因为e==='a'||'b'意思是((e==='a')||'b')

When the e !== 'a' case, it will return 'b' which is truthy in Javascript, so every method got truthy value from each term and then the result will be true.e !== 'a'情况下,它会返回在 Javascript 中为真值'b' ,因此every方法都从每个术语中获取真值,然后结果为真。

You may use the follow to achieve what you want您可以使用以下内容来实现您想要的

const result = str.split("").every((e) => {
    return (e==='a') || (e==='b')
});

You can't omit the === from the second part of your condition.您不能从条件的第二部分省略=== Right now what you're saying is either return e === 'a' or 'b' .现在你说的是 return e === 'a''b' When e is not equal to 'a' it returns 'b' .e不等于'a'时,它返回'b' Which is truthy and when turned into a boolean it becomes true.这是真的,当变成 boolean 时,它变成了真的。 Run this in the browser:在浏览器中运行:

'a' === 'b' || 'c'

If you run it you see that it returns 'c'.如果你运行它,你会看到它返回'c'。 The same happens in your callback.您的回调中也会发生同样的情况。 You have to change your return statement to this for it to work:您必须将您的退货声明更改为此才能正常工作:

return e === 'a' || e === 'b'

Everything seems correct.一切似乎都是正确的。 The problem is in the way you compared the strings问题在于您比较字符串的方式

you should compare those like e === "a" || e === "b"你应该比较那些像e === "a" || e === "b" e === "a" || e === "b"

and the working code和工作代码

const str = "asdfcvb";
const result = str.split("").every((e) => {
    return (e === "a" || e === "b")});
console.log(result);

Explanation:解释:

return e==='a'||'b'

running the test for the first item which is "a" will return true as now e is "a" here, which is equal to "a" .对第一个项目"a"运行测试将返回 true,因为现在e在这里是"a" ,等于"a"

now if it gets to second item which is "s" and obviously not equal to "a" so return e==='a'||'b' becomes return false||'b' and now when you evaluate this the returned value will be "b"现在如果它到达第二个项目,它是"s"并且显然不等于"a"所以return e==='a'||'b'变成return false||'b'现在当你评估这个时返回值将是"b"

and putting "b" as a condition is similar to true cause it is not undefined or null并将"b"作为条件类似于true因为它不是undefined的或null

this can be tested as follows这可以测试如下

if("b") return true; // returns true

Thus it returns true for every item thereby returning true at last因此它为每个项目返回true ,从而最终返回true

You are using the ||您正在使用|| operator in your case incorrectly.操作员在您的情况下不正确。 This is a common typo.这是一个常见的错字。 You are supposed to use the same equal to phrase after ||您应该在||之后使用相同的等于短语. . For example, your code would be like this:例如,您的代码将是这样的:

 const str = 'asdfcvb'; const result = str.split('').every((e) => { return e === 'a' || e === 'b'; }); console.log(result);

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

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