0

I have created this Regex function and it returns false because of the second condition, but I cannot undestand why?

function telephoneCheck(str) {
  if (str.match(/[!?]/) !== null) {
    return false;
  } else if (str.match(/\d/g).length === 11 && str[0] !== 1) {
    return false;
  }
};

console.log(telephoneCheck("1 555-555-5555"));

2
  • Please read this page in its entirety, to make sure people can best help answer your questions. stackoverflow.com/help/how-to-ask Commented Feb 9, 2021 at 7:36
  • This is a syntax here because of extra ( after function. function(telephoneCheck(str) Please make sure to post compilable code. Commented Feb 9, 2021 at 7:39

1 Answer 1

2

str[0] will be the string "1", and you're checking if it equals the number 1. Using === or !== compares the types of the variables as well as the values.

You want str[0] !== "1"

Sign up to request clarification or add additional context in comments.

1 Comment

also you can use != or ==. that type isn't a factor in comparison

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.