JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

function telephoneCheck(str) {
  if (str.indexOf("(") === -1 && str.indexOf(")") > -1){
    return false;
  }
  if (str[0]=== "-"){
    return false;
  }
  
  if (str.indexOf(")") - str.indexOf("(") >= 5){
    return false;  }

  let validNumber = str.replace(/-| /g, "");
  
  if (validNumber.indexOf("(") < validNumber.indexOf(")")){
    validNumber = validNumber.replace(/\(|\)/g, "");
  }

  if(validNumber.length === 10){
    return true;
  } else if (validNumber.length === 11 && validNumber[0] === "1"){
    return true;
  }
  return false;
}

;
console.log(telephoneCheck("55 55-55-555-5"))

cannot seem to pass this test despite all the conditions are being met through my code ( telephoneCheck("55 55-55-555-5") should return false .) it still returns true for some reason.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

Link to the challenge:

Hey, as the input has no parenthesis it does not return false in any of the scenarios you considered.

Here the returned validNumber will be ‘5555555555’ as you are replacing hyphons or spaces. If you check its length is exactly 10 so it will go right through the next if condition.

Thanks for the help , so any suggestions of what should i replace it with?

Tbh I would suggest you use regular expressions for this one