JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

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:

Yeah you have not understood it fully.

For eg this testcase is failing
failed:telephoneCheck("55 55-55-555-5") should return false.

The format of the number is wrong.
55 55-55-555-5

This formatting does not match any of the acceptable formats which are


555-555-5555
(555)555-5555
(555) 555-5555
555 555 5555
5555555555
1 555 555 5555

So you must confirm the format as well as the content.

Allright, thank you a lot! Let’s see what I can do.

I can already feel the regex getting a lot more complicated. Especially with the unpaired brackets. Brain hurts already…

1 Like

You don’t have to make just one if that is too hard. You can also parse it in other ways. Whatever works.

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