Hi guys, can someone please check my code and see what I’ve done wrong.
It says that it needs to return to false, and I can’t seem to figure out the issue.
Your code so far
function telephoneCheck(str) {
if (str.indexOf("(") === -1 && str.indexOf (")") > - 1) return false;
if (str.indexOf(")") - str.indexOf("(") >= 5) return false;
if (str[0] === "-") return false;
let polishedPhone = str.replace(/-| /g, "");
if (polishedPhone.indexOf("(") < polishedPhone.indexOf(")")) {
polishedPhone = polishedPhone.replace(/\(|\)/g, "");
}
if (polishedPhone.length === 10) {
return true;
} else if (polishedPhone.length === 11 && polishedPhone[0] === "1") {
return true;
}
return false;
}
let result = telephoneCheck ("1 (555) 555-5555");
console.log(result);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36
Challenge: Telephone Number Validator
Link to the challenge: