It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
I assume you’re wondering why your code returns true for the 55 55-55-555-5 test case. The code removes all dashes from polishedPhone. So the code can’t check for too many dashes or dashes in the wrong place. The examples have one or two dashes in certain locations. As a previous post mentions, a regex is often used to perform this sort of validation.
If you choose to try and build a regex for the validation, I suggest trying to make a regex that works for the numbers without the parentheses first. After that works, see if you can tweak it to work with the parentheses as well.
If you’re just using a regex, your answer could look something like:
function telephoneCheck(str) {
return / regex goes here /.test(str);
}
It depends on what you want to do. My answer was literally one line inside the telephoneCheck function. The regex was 46 characters long. I did not have any if statements or other code.
If you want to tweak your existing code, you should check for the dashes before they are replaced. This line in your code is removing all the dashes in the phone number.
How about a partial example to help you get started? First copy your code into a text file or something to save it in case you want to try your current method. Then replace all the code with this:
function telephoneCheck(str) {
return /^1?[- ]?\d{3}-\d{3}-\d{4}$/.test(str);
}
telephoneCheck("555-555-5555");
That regex works for some of the test cases.
The ^ at the beginning and the $ at the end force the pattern to be applied to the whole phone number.
1? means look for the number one and it’s ok if it’s not there.
[- ]? means look for the dash or space characters and it’s ok if neither are there.
\d{3} means look for three digits (numbers)
means look for a dash
\d{3} means look for three digits
means look for a dash
\d{4} means look for four digits
You’ll want to modify the regex so that it will work with spaces and parentheses. The spaces and parentheses should only be in certain places in the phone number.
The regex tutorial provides some good experience in creating a regex:
Have you learned regular expressions? You have to declare a variable and assign it a regular expression and then use test method on the regexp variable.
Like this:
Yes, it’s only a starting point. The regex is not complete. You will need to modify it to work with the parentheses and spaces in some of the test cases.