Telephone Validator - Problem with tests?

Tell us what’s happening:
Hi, I am trying to do the JavaScript certification. I am currently on the the Telephone Number Validator challenge. After testing my regular expression on regex101.com, I passed a few of the tests from the challenge and moved the regex over to FreeCodeCamp to run the test.

In my code, I included console.log() for easy debugging to just change the argument of telephoneCheck() and avoid running the tests every time. The problem is that when I run the tests, I get a whole bunch that do not pass. However, when I input those same strings as the function’s argument in the editor, the console prints “true”. Those tests also pass on regex101. Is there a problem with the tests that have been set up for this challenge?

  **Your code so far**

function telephoneCheck(str) {
const regex = /^1?\s?(\((?=\d{3}\)))?\d{3}((?<=\(\d{3})\)|-)?\s?\d{3}(\s|-)?\d{4}$/gi;
console.log(regex.test(str));
return regex.test(str);
}

telephoneCheck("1 555 555 5555");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36

Challenge: Telephone Number Validator

Link to the challenge:

If you remove the console.log then you will pass. Or, if you add a second duplicate console.log then you will pass as well. This has to do with your use of the global (g) flag. I’m too tired to explain it all :slight_smile: My question to you, why do you think you need the global flag on this regexp?

Thanks for the reply! Your suggestion worked. I think the global flag copied over from Regex101… :sweat_smile:

Ahh, gotcha. Ya, it appears that regex101 does have that include by default. Not sure if that’s the best idea.

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