0

My code is failing the test cases "should remove paren, spaces, hyphens, and dots" and "should validate when 11 digits and starting with 1 even with punctuation".

It was previously passing the removal characters case then stopped after adding more stuff and I can't figure out what it is. The logs show pNum stripped of these characters but it's still failing?

Help me debug? Here are the tests/results: https://puu.sh/ziot6/8982a12ad0.png

const phoneNumber = (pNum) => {

  var replaceChars = /[.)( -]/g;

  //removes parenthesis, spaces, hyphens, and dots
  pNum = pNum.replace(replaceChars,"");
  console.log(pNum);

  //returns pNum if length is 11 and starts with 1
  if (pNum.length == 11 && pNum[0] == 1)
      return pNum;

  //returns pNum if length is 11 and starts with 1 and has punctuation
  if (pNum.length == 11 && pNum[0] == 1 && pNum.match(/\W/g))
      return pNum;

  //returns null if pNum is equal or less than 9 or more than 11
  if (pNum.length <= 9 || pNum.length > 11)
      return null;

  //returns null if pNum is 11 without 1 in the first index
  if (pNum.length == 11 && pNum[0] !== 1)
      return null;

  //returns null if pNum has letters or punctuation and less than 11
  if (pNum.match(/[a-z]/i) && pNum.length !== 11)
      return null;

  //return null when area code does not start 2-9
  if (pNum.length == 10 && pNum[0] != ('2','3','4','5','6','7','8','9'))
      return null;
  if (pNum.length == 11 && pNum[1] != ('2','3','4','5','6','7','8','9'))
      return null;

  //return null when exchange code does not start 2-9
  if (pNum.length == 10 && pNum[3] != ('2','3','4','5','6','7','8','9'))
      return null;
  if (pNum.length == 11 && pNum[4] != ('2','3','4','5','6','7','8','9'))
      return null;

}
techie_28
  • 2,123
  • 4
  • 41
  • 62
Keiran
  • 11
  • 2
  • Provide some sample input which are failing – PSK Feb 07 '18 at 04:37
  • Your second test condition will never be true. If it were, you would have already returned `pNum` in the first test. – AuxTaco Feb 07 '18 at 04:39
  • @PSK here's the tests with a log prior to execute and post execute: https://puu.sh/ziot6/8982a12ad0.png – Keiran Feb 07 '18 at 04:59
  • @AuxTaco so I guess I should run the code looking for nonalphanumerics prior? – Keiran Feb 07 '18 at 05:00
  • is the code like `pNum[3] != ('2','3','4','5','6','7','8','9')` valid here?OP seems to be checking for the range and that comparison would always check with the right most value inside the braces i.e 9 in this case? – techie_28 Feb 07 '18 at 06:38
  • @techie_28 it seems to be valid. I did 2-9 and listed out each number and the result was the same. – Keiran Feb 07 '18 at 06:42
  • did you test with `pNum[3]` value anything other than `9`? I dont think that is correct. This comparison will always check with right most value which is 9\ – techie_28 Feb 07 '18 at 06:51

0 Answers0