Its not returning false

Tell us what’s happening:
Describe your issue in detail here. I coded everything correctly
15555555555 true

telephoneCheck("55 55-55-555-5")

should return

false

. // tests completed // console output 5555555555 15555555555 true 15555555555 15555555555 5555555555 5555555555 5555555555 15555555555 5555555 5555555 15555555555 14567894444 123**&!!asdf# 55555555 27576227382 07576227382 27576227382 107576227382 27576227382 27576227382 27576227382 27576227382 (5555555555 555555?5555 5555555555

  **Your code so far**

function telephoneCheck(str) {
if (str.indexOf("(") === -1 && str.indexOf(")") > -1) return false; 
if (str.indexOf(")") - str.indexOf("(") >= 5) return false;
if (str[0] === "-") return false;

let polishedPhone = str.replace(/-| /g, "");

if (polishedPhone.indexOf("(") < polishedPhone.indexOf(")")) { 
polishedPhone = polishedPhone.replace(/\(|\)/g, "");
}

console.log(polishedPhone)
if (polishedPhone.length === 10) {
return true;
} else if (polishedPhone.length === 11 && polishedPhone[0] === "1") {
return true;
}

return false;
}
let result = telephoneCheck("1 (555) 555-5555")
console.log(result);

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36 Edg/92.0.902.84

Challenge: Telephone Number Validator

Link to the challenge:

Does this help?

You are doing this in a wrong way. You have to use a RegExp to complete this project.

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.

What line would I write this after?

Like where would I write this at?

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);
}

I have all those already

What line would I put this at though

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.

let polishedPhone = str.replace(/-| /g, "");

Would you be able to show me an example

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:

You can use this website to create complex regular expressions. It really helps a lot to create regexp.
RegExr: Learn, Build, & Test RegEx

It’s saying telephone check has already been declared

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:

function telephoneCheck(str) {
const regExp =  /^1?[- ]?\d{3}-\d{3}-\d{4}$/
  return regExp.test(str);
}

telephoneCheck("555-555-5555");

The answer should only have the code. Here is a screenshot to show what the screen should look like.

Is this the only thing that needs to be on here?

Also how should the cash register one look?

That code for the telephone one did not complete the project

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.