Tell us what’s happening:
Hi! This is my first ever post here, so i might sound a little confusing - and for that i am sorry. I swear i’m trying my best to be clear.
I know this code might be a little bit of a mess (i commented some parts of it for clarity) but i thought it’d pass the tests - at least the one with “conventional” phone number formats. It passes some of the tests, but it does not pass other tests which i deem similiar, and i don’t even know what to look for.
For example, all of the function tests for
telephoneCheck("1(555)555-5555")
should returntrue
.
return as they should for a true result, but the function returns “false” in the end.
Your code so far
function telephoneCheck(str) {
//check if, without non-characters, its all numbers
//make it an array
//check if length<12
//if array.length>10, check if first number is 1
//make it an array
// remove all non-characters
const replaced = str.replace(/[^a-z0-9]/gi, '')
let numberArray = replaced.split("");
console.log("number test" + " " + /^[0-9]+$/.test(replaced));
console.log("replaced length" + " " + replaced.length);
console.log("first index of array" + " " + numberArray[0]);
function seriesOfTests(str) {
//check if, without non-characters, its all numbers
if (/^[0-9]+$/.test(replaced)) {
//check if length<12
if (replaced.length < 12) {
//if array.length>10, check if first number is 1
if (replaced.length === 10 || (replaced.length === 11 && numberArray[0] === 1)) {
// if all of that passes, return true
return true
} else {
return false
}
} else {
return false
}
} else {
return false
}
};
console.log("FINAL RESULT" + " " + seriesOfTests(str))
return (seriesOfTests(str))
}
telephoneCheck("1(555)555-5555");
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Challenge: JavaScript Algorithms and Data Structures Projects - Telephone Number Validator
Link to the challenge: