Validate US Telephone Numbers (regex compiler issues)

Validate US Telephone Numbers (regex compiler issues)
0.0 0

#1

Tell us what’s happening:
it seems to me that there are some issues with the regex compiler on the site my current solution (not as clean as it could be i know) passes all unit tests, but it will not work on code camp.

https://regex101.com/r/AlyWyI/1 - evidence of unit tests passed

Your code so far

function telephoneCheck(str) {
  if (str == /^(1\s{0,1}\-{0,1}((\(\d{3}\))|(\d{3}))|((\(\d{3}\))|(\d{3})))\s{0,1}\-{0,1}\d{3}\s{0,1}\-{0,1}\d{4}$/){
    return true;
  }else{
    return false;
  }
}



telephoneCheck("555-555-5555");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/65.0.3325.181 Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:


#2

str is a String and is a RegEx object. When you try to compare them the way you are, it will always return false. Look into thetest method for how to do this properly.

FYI - When you have an if statement which returns a true or false value, do not use if/else. Instead, just return the expression you want evaluated. See below for an example:

// Original if/else
var color = "blue";
if (color === "blue") {
  return true;
}
else {
  return false;
}

A better way:

var color = "blue";
return color === "blue"; // returns true