Results seems good when logged but test won't pass

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

Hi Everyone,

I came up with this regex to solve this challenge and was kind of proud of myself with it :slight_smile:
However the test won’t pass, i realy don’t get why, as you can see in my code, i log what the function return before returning it and to me, it seems that true is returned when needed and false also.

It seems all truthy tests are not passing…

Thx for your help regarding this, i’m on this regex since 2 days and i might be to close to the subject to see something realy evident for someone with more distance on the code.

Sorry for my english, foregner…

  **Your code so far**

function telephoneCheck(str) {

let killerRegex = /^1?(([\s\-]?\d{3}[\s\-]?)|(\s?\(\d{3}\)\s?))\d{3}[\s\-]?\d{4}/gm
console.log('----------------------------------------')
console.log(str)
console.log(killerRegex.test(str))
console.log('----------------------------------------')
return killerRegex.test(str);
}

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

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0

Challenge: Telephone Number Validator

Link to the challenge:

Run it this way:


function telephoneCheck(str) {
  let killerRegex = /^1?(([\s\-]?\d{3}[\s\-]?)|(\s?\(\d{3}\)\s?))\d{3}[\s\-]?\d{4}/gm
  console.log('----------------------------------------')
  console.log(str)
  console.log(killerRegex.test(str))
  console.log('----------------------------------------')
  return killerRegex.test(str);
}

console.log(telephoneCheck("1 555-555-5555"));

As a hint: One of your flags is causing a problem - figure out why.

Hi Kevin,

Thx for your answer, i had not think of this, i tried my regex en 101 regex and copy it, might be that the engines work differently there…
I removed the global flag and that resulted in other problems, some tests weren’t good, i had to force the end to be a group of 4 digits to make them go.

In fact, problem solved, have a nice day !

1 Like

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