Telephone Number Validator

Hi all, Somehow my code is not passing for 11 digit phone number that starts with 1. The code is bouncing after-

if(numCount === 11 && str[0] === 1)

Following is the code-

function telephoneCheck(str) { 
  str = str.replaceAll(" ", "");
  let numCount = 0;
  for(let i = 0; i < str.length; i++){  
    debugger;
    if(parseInt(str[i]) === Number(str[i])){
       numCount += 1
    }    
  }
   if(numCount === 10){
     if (str.length === 10) {
     return true
   } else if (str[0] === "(" && str[4] === ")" 
       && str[8] == "-"){
     return true
   } else if (str[3] === "-" && str[7] === "-"){
     return true
   }
} else {
     if(numCount === 11 && str[0] === 1) {
        if(str.length === 11){
         return true
      } else if (str[1] === "(" && str[5] === ")" 
                 && str[9] == "-") {
        return true
     } else if (str[4] === "-" && str[8] === "-"){
           return true
     } 
   } 
}
 return false 
}

telephoneCheck("1 555-555-5555");

Following is the link to the problem-

Thank you!!

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Please can you provide more details.
What should your function do, what is doing, what do you expect to happen, why do you think it is failing?

The code is bouncing after a line for phone numbers that starts with 1, hence returning false.

I immediately started edited the code after I posted it. Sorry about that though.

 } else {
   console.log("inside else")
console.log({numCount, str, "str[0]": str[0]})
     if(numCount === 11 && str[0] === 1) {
   console.log("inside if")
console.log({numCount, str, "str[0]": str[0]})
        if(str.length === 11){
         return true
      } else if (str[1] === "(" && str[5] === ")" 
                 && str[9] == "-") {
        return true
     } else if (str[4] === "-" && str[8] === "-"){
           return true
     } 
   } 
}
 return false 
}

telephoneCheck("1 555-555-5555");

I would start debugging, for example like this, until you figure out what execute and what doesn’t and why

And then continue, like

console.log({
   numCount,
   str,
   "str[0]": str[0],
   "numCount === 11 && str[0] === 1": numCount === 11 && str[0] === 1,
   "numCount === 11": numCount === 11,
   "str[0] === 1": str[0] === 1
})

Until you find what is that gives the wrong value

Thanks for introducing this form of consoling. Very useful. Thanks again @ILM :star2:

I see where I made a mistake. Thanks. Also, when I do console.log for str[0] it gives me 1 not “1”.

Edit: But when I put typeof function on str[0] then console shows string.

it uses the Object property shorthand, and in this way the logged values are easily labelled

you can also use console.table if you use the browser console

1 Like

For NANP (North American Number Plan), you’ll want to check the 1st, and 4th number of a 10 digit number to make sure they are 2-9. There will never be a 0 or a 1 in those positions. If you’re looking for country code (11 digis), then it will be positions 2 and 5.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.