Skip to content
Advertisement

Why does my test pass, even though it doesn’t meet my logic gate?

I’m working on telephone validator on FCC. For some reason this passes 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. 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");

Advertisement

Answer

You need to restate the condition you’re comparing (|| '(' will always be 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

Advertisement