I am failing the if this bracket: ‘(’ exists then ‘)’ should also exists.
Is there a way to use if…then in regEx.
I can forcefully pass the test here by creating another reg ex that counts the number of brackets and if its less than one then return false but I think that would be sort of cheating.
I also want to ask if in the current code, do you find anything forcefully making correct or not
**Your code so far**
function telephoneCheck(str) {
let newStr=str.split("");
newStr=newStr.filter(a=>/\d/.test(a));
let regEx=/^(?!-)1*\s*?\(?\d{3}\)?-?\s?\d{3}\s?-?\d{4}(?!(\))$)/;
if (regEx.test(str) && (newStr.length===10 || (newStr.length===11) && newStr[0]==='1')){return true;}
return false;
}
console.log(telephoneCheck("-1 (757) 622-7382"));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 OPR/76.0.4017.177
Challenge: Telephone Number Validator
Link to the challenge: