Tell us what’s happening:
My code doesnt seem to work for the test case telephoneCheck("11 555-555-5555")
. It should actually be false, since I check with re1 and re3 that the first number is 1, right? Or does [1]
check if the there are several 1’s in this string?
Your code so far
function telephoneCheck(str) {
var strArr = str.split("")
const re1 = /[1][\s-]\d{3}\s\d{3}\s\d{4}/
const re2 = /\d{3}-\d{3}-\d{4}/
const re3 = /1 \(\d{3}\)\d{3}-\d{4}/
/*var strMap = strArr.filter((x) => x.match(/\d/g))
console.log(strMap)
if(strMap.length == 11){
return strMap[0] == 1
}
else if(strMap.length == 10){
return true
}
return false;
}*/
return re1.test(str) || re2.test(str) || re3.test(str) || str.length==10
}
telephoneCheck("555-555-5555");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.81
Challenge: JavaScript Algorithms and Data Structures Projects - Telephone Number Validator
Link to the challenge: