Why is this number invalid?

Tell us what’s happening:
Hello, so I was writing the code to this challenge. and i still had one number which had to return false, but returned true with me.

the number is: “6054756961”

I know I have tackled this problem in a very different way than others; however, my question is why is this number not valid. According to the main formats, shouldn’t this number be a valid number.

so my problem is not how to solve the challenge, but instead why this number in general is invalid. This supposedly will help me solve the last part of the challenge.

Your code so far


function telephoneCheck(str) { 
let temp = str.split(' ').join('')

if (temp[0] == '-') {
  return false
}

temp = temp.split('-').join('')

if (temp.match(/[(]/)) {
  if (temp.match(/[)]/)) {
    temp = temp.split('(').join('').split(')').join('')
  } else {
   return false 
  }
}

if (temp.match(/[)]/)) {
  return false 
}

if (temp.match(/\D/)) {
  return false
}

if (temp.length == 10) {
  return true
} else if (temp.length == 11) {
  if (temp[0] != 1) {
    return false
  } else {
    return true
  }
} 

return false
}

console.log(telephoneCheck("6054756961"));

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 7.0; T10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.12 Safari/537.36.

Challenge: Telephone Number Validator

Link to the challenge:

The string presented in the test is “(6054756961)”. This is not a phone number as defined by the tests.

Walk through your function manually and see where you end up. Specifically your second if statement.

2 Likes

Thanks very much.
I understood the problem.
I’d like to ask, please be honest, is the way I am trying to solve this challenge too strange or inefficient.

It is not strange, but most of your conditionals could be combined to make it a bit more condensed.

I think the spirit of this challenge is to get you thinking in the right way which you are doing fine.

here is a condensed version of your code.

function telephoneCheck(str) { 
  let temp = str.split(' ').join('')

  if (temp[0] == '-') {
    return false
  }

  temp = temp.split('-').join('')

  if (temp.match(/1?\(\d{3}\)/)) {
      temp = temp.split('(').join('').split(')').join('')
  }

  if ((!temp.match(/[)]/) || !temp.match(/\D/)) && temp.length == 10 || temp.length == 11 && temp[0] == 1) {
    return true
  } 

  return false
}

console.log(telephoneCheck("(6054756961)"));
1 Like

Ok thanks very much for your help.