JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

Tell us what’s happening:
My code doesnt seem to work for the test case telephoneCheck("11 555-555-5555"). It should actually be false, since I check with re1 and re3 that the first number is 1, right? Or does [1] check if the there are several 1’s in this string?
Your code so far

function telephoneCheck(str) {
  var strArr = str.split("")

  const re1 = /[1][\s-]\d{3}\s\d{3}\s\d{4}/
  const re2 = /\d{3}-\d{3}-\d{4}/
  const re3 = /1 \(\d{3}\)\d{3}-\d{4}/
  
  /*var strMap = strArr.filter((x) => x.match(/\d/g))

  console.log(strMap)

  if(strMap.length == 11){
    return strMap[0] == 1
  }
  else if(strMap.length == 10){
    return true
  }

  return false;
}*/

return re1.test(str) || re2.test(str) || re3.test(str)  || str.length==10 

}

telephoneCheck("555-555-5555");

Your browser information:

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

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

Link to the challenge:

The square brackets create a character class. It matches any of the characters in the brackets. But just once. Since you only have one character in the brackets you don’t need the brackets.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.