JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

Cant seem to remember which regex will make the failed challenge a false boolean? Please help

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

Your code so far

function telephoneCheck(str) {
  const validated = [
    /*555-555-5555*/
    /^\d{3}-\d{3}-\d{4}$/,
    /*(555)555-5555*/
    /^\(\d{3}\)-\d{3}-\d{4}/,
    /*(555) 555-5555*/
    /^\(\d{3}\)\s\d{3}-\d{4}/,
    /*555 555 5555*/ 
    /^\d{3}\s\d{3}\s\d{4}/,
    /*5555555555, done*/
    /^\d{10}/,
    /*1 555 555 5555*/
    /^1\s\d{3}\s\d{3}\s\d{4}/,
    /*1 555-555-5555*/
    /^1 \d{3}-\d{3}-\d{4}/,
    /*1 (555) 555-5555*/
    /^1\s\(\d{3}\)\s\d{3}-\d{4}/,
    /*(555)555-5555*/
    /^\(\d{3}\)\d{3}-\d{3}/,
    /*1(555)555-5555*/
    /^1\(\d{3}\)\d{3}-\d{4}/,
    /*27576227382*/
    /\\d{11}/
  ]
  return validated.some((pattern) => pattern.test(str))
}

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/106.0.0.0 Safari/537.36

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

Link to the challenge:

Don’t you want to make sure that all of the patterns in validated array are true? If so, you should look into the every method instead of the some method.

This project can be solved with a single regex pattern.

Wow! That is a lot of regex patterns! You don’t think one pattern could have been enough?

One pattern probably would but I understood this better. It has ounces of logic.

Your current code will not work with every either because depending on the telephone number received as the argument, all of the patterns would need to be true to return true for the telephone number. You need to rethink your algorithm here.

1 Like

Can I use the look ahead character?

Sure you can use it but it will depend on how it is used. I would nix the array of regexes and start thinking about how to solve with a single regex, though you do not technically have to use any regular expressions to solve the challenge. However, I would use regex to solve.

1 Like

I can only think of if statements and switch? but i’ll see whats happening. Thanks!

555-555-5555
(555)555-5555
(555) 555-5555
555 555 5555
5555555555
1 555 555 5555

/^1?(\s\d{11}\s\d)$/g

How is this?

Plug your regex pattern and strings in this online tester https://regex101.com/

1 Like