Criticize my solution [Telephone Number Validator]

Here is the problem is known as, Telephone Number Validator inside JavaScript Algorithms and Data Structures Certification Curriculum.
Here is my code is given below :

function telephoneCheck(str) {
  //we will look for the valid pattern inside warrior
  let warrior;
  //we are checking it's begin with 1 or not
  if(str[0] === '1'){
    if(str[1] === " "){
      warrior = str.slice(2);
    }else{
      warrior = str.slice(1);
    }
  }else{
    warrior = str;
  }
  let wLen = warrior.length;
  
  //we are looking for pattern inside warrior
  if(wLen === 12){
    return (warrior[3]==="-" && warrior[7]==="-")||(warrior[3]===" " && warrior[7]===" ");
  }else if(wLen === 13){
    return (warrior[0]==="(" && warrior[4]===")" && warrior[8] === "-");
  }else if(wLen === 14){
    return ((warrior[0] === "(" && warrior[4] === ")" && warrior[5] === " " && warrior[9] === "-")||(warrior[1] === " " && warrior[5] === " " && warrior[9] === " "));
  }else if(wLen === 10){
    let c = 0;
    for(let i=0; i<warrior.length; i++){
      if(typeof(parseInt(warrior[i])) === "number"){
        c += 1;
      }
    }
    if(c === warrior.length){
        return true;
      }else{
        return false;
      }
  }
  else{
    return false;
  }
}

Please share your idea to make my code more better :gift_heart:

If it works, that is a good first step.

Suggestion?
Use regex for that particular task.
It should work faster and be a little bit more readable.

1 Like