US Validate Number Chall

US Validate Number Chall
0.0 0

#1

Hi Fellows, Hope you all good. I was working on the US validate number Challenge and I have something I don’t understand. Here is my Code and it gives False result and I don’t know why. Thanks in Advance :slight_smile:

BS: I am not a US citizen so I don’t know rules of the right US number format



function telephoneCheck(str) {
  if (str[0] == "-" && str[1] ==1){
    return false ;
  }
  if (str[0] == "(" && str[1] !== 5){
    return false;
  }
  var re = str.replace(/\s/gi ,'').replace(/[A-z]/gi,'').replace(/\W/gi , '');
  var convertor = Number(re);
  if (str.indexOf(")") !== -1 && str.indexOf("(") == -1){
    return false ;
  } else if (str.indexOf("(") !== -1 && str.indexOf(")") == -1){
    return false ;
  }else 
  {if (re.length < 10){
    return false ;
  }else if (re.length > 11 ){
   return false ;
  }else if (re.length == 11 ){
   return re[0] == 1 ;
  }else if (re.length == 10){
    return convertor !== null;
  }if (typeof str[0] !== "number"){
    return false ;
  }
  }
  //return str.indexOf(")");
  //return re.length  ;  
  //return typeof convertor === "number" && convertor !== null;
}



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



#2

The following code is causing the function to return false.

  if (str[0] == "(" && str[1] !== 5) {
    return false;
  }

Both of the if conditions evaluate to true, so the return false statement is executed.


#3

Ops! I forgot the parentheses! Thank you :slight_smile: