If statement returning true when it's supposed to be false

Tell us what’s happening:

I’m working on Telephone Number Validator challenge and what I’m trying to do is to get the Boolean return true when the phone number is equal to 10 digits and if the phone number is equal to 11 digits and the first digit starts with number 1, also return true. Otherwise return false.

Instead, what I’m getting is they all return false, totally ignoring my conditional statements. I’m positive that the logical steps should be correct, but how I implement it is probably not the right way.

Your code so far


function telephoneCheck(str) {

if (str.length === 10) {
  return true
  } else if (str.length === 11 && str[0] === '1') {
  return true
  } else {
    return false
  }
}

// should return false 
// has letters (a-z)
console.log(telephoneCheck("123**&!!asdf#"))
// less than 10 numbers
console.log(telephoneCheck("555-5555"))
// not no.1 at the start
console.log(telephoneCheck("2 (757) 622-7382"))

// should return true
console.log(telephoneCheck("555-555-5555"));
console.log(telephoneCheck("1 (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/87.0.4280.141 Safari/537.36.

Challenge: Telephone Number Validator

Link to the challenge:

add console.log(str.length) as first line in your function and see if it is the number you expect

1 Like

Hello there, I have seen your code and its true you are getting false. The code you have written also checks for whitespaces, brackets and also the hyphen in the string.

1 Like

The whitespace, brackets and hyphens also count as part of the length of the string.

1 Like

Oh yeah, you’re right! Now that I’ve logged the answers, it turns out that other symbols are also accounted for. I should have eliminated them first before adding the if statements

Glad you’ve seen them. Good luck with your project

1 Like

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