[英]Why does my test pass, even though it doesn't meet my logic gate?
I'm working on telephone validator on FCC. 我正在FCC的电话验证器上工作。 For some reason this passes
5555555555
. 由于某种原因,这会通过
5555555555
。 Why does my logic gate pass this number? 为什么我的逻辑门会通过这个数字? For context, this isn't my first attempt at this code.
对于上下文,这不是我第一次尝试此代码。 I've added multiple statements, nested if statements, and it still doesn't catch it.
我添加了多个语句,嵌套了if语句,但仍然无法捕获它。 Why does this evaluate to true?
为什么这算是正确的? Here's the code:
这是代码:
function telephoneCheck(str) {
if(str[0] === '1' || '(' && str.length >= 10) {
return true;
}
else {
return false;
}
}
telephoneCheck("5555555555");
You need to restate the condition you're comparing ( || '('
will always be true): 您需要重述要比较的条件(
|| '('
始终为true):
if(str[0] === '1' || str[0] === '(' && str.length >= 10) {
This is due to the fact that &&
has a greater precedence than the ||
这是由于
&&
的优先级高于||
的事实。 operator. 操作员。 So without the parenthesis, the
'(' && str.length >= 10
part of the expression is evaluated first. So the ultimate condition becomes str[0] === '1' || true
which would always be true. So your code would return true for any string of length >= 10 因此,如果没有括号,将首先计算表达式的
'(' && str.length >= 10
部分。因此最终条件变为str[0] === '1' || true
,这将始终为true。对于长度大于等于10的任何字符串,代码将返回true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.