Tell us what’s happening:
There is either something I don’t understand about US phonenumbers and how they are presented or I don’t understand what the challenge wants me to do.
Why should the last two test cases in the code snippet return false instead of true?
They both have maximum of 11 digits in them including country code which is the max length with country code.
So removing all the “excess” special characters gives me all the numbers regardless if someone has typoed the number with only opening or closing bracket or with a extra character in front of the number.
I’m pretty sure I haven’t understood the challenge correctly.
Your code so far
function telephoneCheck(str) {
let regex = /\D/g;
let num = "";
for (let i = 0; i < str.length; i++) {
if (str[i].match(regex)) {
num += "";
} else {
num += str[i];
}
}
console.log(num)
if (num[0] == 1 && num.length == 11 || num.length == 10) {
return true;
}
return false;
}
telephoneCheck("555-555-5555");
telephoneCheck("1 555)555-5555");
telephoneCheck("-1 (757) 622-7382");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Challenge: JavaScript Algorithms and Data Structures Projects - Telephone Number Validator
Link to the challenge: