Why my code can't pass the test?

**Why my code can’t get false in this test case:
telephoneCheck("(6054756961)");
**

  **Your code so far**

function telephoneCheck(str) {

let checkNum = str.replace(/[^\d]/g, "");
let hasPermittedCharsOnly = str.match(/[\s|\-]/g);
let twoOr0 = str.match(/[/(/)]/g);

 if(checkNum.length === 10 || (checkNum.length === 11 && str.charAt(0) == 1)){
  
   if( hasPermittedCharsOnly === null){
    return true;
  }else if(hasPermittedCharsOnly.length + checkNum.length == str.length){
    return true;
  }else if(twoOr0.length === 2 && (/\d$/).test(str) && (str.charAt(0) === "(" && str.charAt(4) === ")") || ((str.charAt(1) === "(" && str.charAt(5) === ")")) || (str.charAt(2) === "(" && str.charAt(6) === ")")){
    return true;
  }else{
    return false;
  }
}else{
   return false;
}
}
 
telephoneCheck("(6054756961)");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36.

Challenge: Telephone Number Validator

Link to the challenge:

Your code is not accounting for the fact that ( and ) can only surround 3 numbers that are at the beginning of the string, appear after a 1 or after a 1 . This particular string has all 10 digits between the ( and ) which is not a valid US phone number.

Thank you, RandellDawson,
With this case in my browser, I get false, but in the code tester of the task, I didn’t get false. Why?

let twoOr0 = str.match(/[/(/)]/g);
if((/\(\d{3}\b\)/).test(str) && twoOr0.length === 2){
      return true;
    }else{
      return false;
    }

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.