Telephone Number Validator - Giving FCC Forums another chance

Telephone Number Validator - Giving FCC Forums another chance
0

#1

Tell us what’s happening:

OK, So I have been encouraged to give the mods and the forum another try, so here it is. Can someone help me understand why my code doesn’t pass all the tests? Specifically it says one of the failed tests was “1 456 789 4444”.

My code has 1 as optional. Check. It has a space as optional. Check. It takes 3 numbers. Check. Again, it accepts a space. Check. It takes 3 additional numbers. Check. Another space. Check. And finally 4 numbers. Check.

Further more, its actually better than the “basic” solution FCC gives.


/^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/

and here is why. If someone enters 11 to start a phone number, that means something. No USA area codes start with a 0 or a 1, yet the FCC solution would accept that. Wrong.

Additionally, I crafted my RegEx on RegEx101.com.I created a bunch of tests myself, and it passed them all. When FCC says it doesn’t, I took the 7 or so and put them into RegEx101 and it says it does pass.

*******I do not want to hear anyone telling me to post on GitHub. ******
******* If that is what you want to write, don’t! *******

Your code so far


function telephoneCheck(str) {
  let regEx = /^[1]?[\s\-\.]?(\([2-9][0-9][0-9]\)|[2-9][0-9][0-9])[\s\-\.]?\d{3}[\s\-\.]?\d{4}$/;
  if (str == regEx) return true;
  return false;
}

telephoneCheck("555-555-5555");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/telephone-number-validator/


#2

Your str variable will never equal regEx, because one is a string and one is a regular expression.

If you want to test that str matches your regular expression, read about RegExp.prototype.test.


#3

Then why does it pass all but 7? Yes, I can modify my code to regex.test(str) and I would have if it failed all the test.


#4

Are you talking about your existing code passing all but 7 or are you talking about using the RegExp.protottype.test method passes all but 7?


#5

The code you posted above passes all but 7 of the tests, because all but 7 of the tests should return false. Your code returns false for every single test case because of the reason I told you in my first reply.

If you use regEx.test(str) correctly, you will pass all the tests.

So why does your posted code pass all but 7? Pure chance.


#6

The reason I came to post, was because the code I had (original) passed some tests. Looking at it now, I see it only passed the “false” tests. Which means I didn’t need to come post, but I also wanted to follow Quinceys suggestion to give you all another try.

When I switched the code to .test() it passed them all. I wasn’t paying attention because I had built it in Regex and didn’t catch that I should use test. Once it didn’t pass, and after I posted, I changed it to .test and it passed.

Anyway, like I said, my bad. I should’ve tried test before posting. But, still, the comment about the basic solution is valid. 0 and 1 and not valid in certain situations. 1-0 or 1… not valid. 0or1… not valid.


#7

Does this mean you want to delete this topic?


#8

Well, I’ve asked that for the others and I’ve been ignored. Don’t get why the response now would be different. But sure. It doesn’t need to be here unless the 0 and 1 comments are seen as valid.


#9

I will leave the post.