简体   繁体   English

为什么我的测试没有通过我的逻辑门,为什么仍能通过?

[英]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.

相关问题 如果我的表格不符合条件,为什么会一直发送? - Why does my form keep sending if it doesn't meet the condition? Ember js:在我的示例中,即使路由器中使用的功能尚未完成,为什么路由器中的逻辑仍然继续? - Ember js: In my example why does the logic in the router continue even though the function used in it has not completed? 为什么我的图像在点击时不会改变,即使逻辑看起来很简单? - Why won't my images change when clicked even though the logic seems pretty straight forward? 为什么我的链表输出未通过此测试? - Why doesn't my linked list output pass this test? 为什么我的 LED 灯开始闪烁,即使一开始没有闪烁? 微:位 JavaScript - Why do my leds start flicking on and off, even though it doesn't do it at the start? Micro:bit JavaScript 为什么即使我实现了 componentWillUnmount,我的组件也没有卸载? - Why doesn't my component unmount even though I implemented componentWillUnmount? 为什么即使未提供令牌,我的测试也会收到状态 200? - why is my test receiving status 200 even though token is not provided? 为什么我的循环没有停止,即使它已经结束了? - Why does my loop not stop, even though it's at the end? 为什么我的程序表现为好像在服务器上找不到图像? - Why does my program act as though it doesn't find images on the server? 即使质量门被违反,SonarQube也不会破坏Jenkins的构建 - SonarQube doesn't break Jenkins build even though Quality Gate is violated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM