我的代码未通过测试用例“应删除paren,空格,连字符和点”和“应该验证11位数字,并以1开头,即使是标点符号”。
以前通过删除字符的情况然后在添加更多东西后停止,我无法弄清楚它是什么。日志显示pNum剥离了这些字符,但它仍然失败了?
帮帮我调试?以下是测试/结果: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;
}