Hello, I’ve been working on the 4th project on the JavaScript and Algorithm course for the past three days (Telephone validator). Seen a lot of solutions online, but I really want to go ahead with the algorithm I’m currently using.
I’m trying to solve it using the if…else method, but it only validates the ones that return false. I don’t know where this is coming from.
My code so far
function telephoneCheck(str) {
let tenDigits= str.match(/^\d{10}$/);
let elevenDigits = str.match(/^\d{11}$/);
let startsWithOne=str.match(/^[1]$/);
let permittedChars=str.match(/^[\\d() -]+$/);
if(tenDigits && permittedChars){
return true;
}else if(elevenDigits && startsWithOne && permittedChars){
return true
}else
return false;
}
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/75.0.3731.0 Safari/537.36
.
Challenge: Telephone Number Validator
Link to the challenge: