# JavaScript Algorithms and Data Structures Projects Telephone Number Validator

Pretty sure this isn’t a good way to complete these challenges, but this sort of stuff is what my brain defaults to, assuming I can even come up with something functional in the first place. I spend a lot of time just looking stuff up for each assignment just to figure out things I’ve already been over. Does this stuff ever start to come naturally, or am I just not cut out for it?

Also if anyone has tips on how to improve this solution, maybe that’ll help me learn something, and I’d appreciate the effort.

function telephoneCheck(str) {

if ((/^1\s([0-9]{3})\s([0-9]{3})\s([0-9]{4})\$/).test(str)) {

return true;

} else if ((/^1\s([0-9]{3})-([0-9]{3})-([0-9]{4})\$/).test(str)) {

return true;

} else if ((/^1\s\(([0-9]{3})\)\s([0-9]{3})-([0-9]{4})\$/).test(str)) {

return true;

} else if ((/^1\(([0-9]{3})\)([0-9]{3})-([0-9]{4})\$/).test(str)) {

return true;

} else if ((/^\(([0-9]{3})\)([0-9]{3})-([0-9]{4})\$/).test(str)) {

return true;

} else if ((/^([0-9]{3})-([0-9]{3})-([0-9]{4})\$/).test(str)) {

return true;

} else if ((/^([0-9]{3})([0-9]{3})([0-9]{4})\$/).test(str)) {

return true;

} else if ((/^([0-9]{3})\s([0-9]{3})\s([0-9]{4})\$/).test(str)) {

return true;

} else if ((/^\(([0-9]{3})\)\s([0-9]{3})-([0-9]{4})\$/).test(str)) {

return true;

} else {

return false;

};

}

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

Your solution passes the tests, so congrats on getting a working solution.

I think you could work on trying to generalize your pattern matching a little more so you don’t have to have a separate pattern for every possibility. I was able to solve this challenge with only one pattern. The way I went about this is to start by matching the basic telephone number pattern:

/^\d\d\d\-\d\d\d\-\d\d\d\d\$/

And then looking at the tests that failed and modifying the pattern to take those failures into account. A few hints:

• Don’t forget about the pipe (|) which allows you to do either/or matching
• Question marks will be your friend in this one

Good luck.

Ahhhh, I was trying to use the || symbol in my original idea, which was going to be just one if statement kinda like this.
if (str.match(regex || regex || regex…) === 1) {
return true;
} else {
return false;
}

The problem I had there was that the || sign kept removing the functionality of the slashes wrapping the regexes I think.

Regardless, thanks for the tips!

Just to be clear, I wasn’t referring to the double pipe OR operator in JS. I was referring to the single pipe OR operator in regex expressions.

Also, I just had one single regex expression.

Good job figuring it out. I went ahead and added spoiler tags to your solution.

It definitely does. “Practice makes perfect” applies here without equivocation.

Also, if you want to learn about what other solutions there are, here’s one I had: Phone number Validator. Not able to pass 3 tests. Please help - #4 by gaac510
Hopefully it can help you learn a thing or two.

you will always look up stuff so even if coding in general gets easier, or you will find harder challenges, looking up stuff is something that you always do

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