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
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.
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