Telephone Number Validator - never seen anything like this :(

I did not go the route of regex. Partly b/c I hate regex (who doesn’t) but mostly b/c it was not the way I thought through the pseudo-code as you can see from my code below.

My question pertains to a specific testcase that is failing:

telephoneCheck("555-5555") should return false.

When I add in a bunch of console.logs to pin down where it is going wrong, I am able to conclude the following:

After the for loop, the onlyNum array looks like this: 5,5,5,5,5,5,5. As expected.
And it’s length is: 7. Also, as expected.

Then quite inexplicably, when I start testing whether onlyNum has 10 or 11 digits, onlyNum becomes this: ["5", "5", "5", "5", "5", "5", "5", empty × 3]. With a length of 10.

What changed between the 2 console.logs…??

Your code so far

function telephoneCheck(str) {
//Check all are numbers
    //If yes, check that number digits are:
      //if 10 return true
      //if 11, check first number is 1; return true
    //anything else; return false

  //If not, check if non-numbers are ( or ) or -
    //If yes, check that number digits are:
      //10; return true
      //if 11, check first number is 1; return true
    //anything else; return false

    let strAsArray = str.split("");  
    let onlyNum = [];

    for(var i = 0; i < strAsArray.length; i++){
      let acceptableChar = (strAsArray[i] === "-" || strAsArray[i] === "(" || strAsArray[i] === ")")
        if(!isNaN(strAsArray[i])){
          onlyNum.push(strAsArray[i]);
        }else if(!acceptableChar) {
          return false;
        }
    }

    console.log("This is onlyNum: " + onlyNum);
    console.log("And this is onlyNum length: " + onlyNum.length);

    if(onlyNum.length = 10 || (onlyNum.length = 11  && onlyNum[0] == 1)){
        console.log(onlyNum);
        console.log("The length of " + onlyNum + "is: " + onlyNum.length);
        return true;
    }

    return false;
}



// telephoneCheck("555-555-5555");

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/telephone-number-validator

Your problem is here:

if(onlyNum.length = 10 || (onlyNum.length = 11 && onlyNum[0] == 1)){

You are using the assignment operator (=) instead of a comparison operator (== or ===). When you evaluate onlyNum.length = 10 is is simply trying to assign that value into that property and then returning that value. Since 10 evaluates as truthy, it will always be true.

1 Like

And for one, I love regex. I know it is arcane and confusing, but it is also incredibly powerful. It’s worth the struggle to at least get familiar with it.

3 Likes

I got the code to where 7 testcases are failing.

First set of testcases: I understand why these should return true:

telephoneCheck("1 555 555 5555") should return true.

telephoneCheck("1 456 789 4444") should return true.

And I tried to fix my code by accounting for spaces. But I keep running into a problem with my isNaN code. I know that isNaN is finicky. So after some googling, I found that JS interprets " " as 0 and therefore fails isNaN test. The remedy suggested was to use parseInt which I did. To no avail. Any other ideas?

Second set of testcases: I don’t understand why these testcases should result in false:

telephoneCheck("(6054756961)") should return false
telephoneCheck("555)-555-5555") should return false.
telephoneCheck("(555-555-5555") should return false.

If we removed the parantheses and hyphens from the number, we’d end up with a 10 digit number which means it is a valid telephone number…no? If we are assuming that (555)555-5555 is a valid telephone number, we are accounting for the fact that ( and ) and - are not part of it. So why can’t we do the same for "(555-555-5555"…?

Thanks.

Because (555-555-5555 is not a valid telephone number. Period. Maybe if you were designing the specs for the app you would want to accept it. But you are not designing the specs. And that is often the way of web dev. Where I work, I often get specs from the UI/UX people and think, Man, if they just let me do this or accept that, it would be sooooo much easier. But we often have to meet the specs that other people give us.

To put it bluntly, your job isn’t to change the specs to fit you abilities. Your job is to raise your abilities to meet your specs.

And I still think you are passing up a great opportunity to learn about regex. It is like someone is trying to open a bottle with a screwdriver, some duct tape, and a mule team. And you’re trying to tell them to just lean how to use a bottle opener.

Yes, regex is a very confusing bottle opener. And you might have to divert off FCC and consult some outside materials to find what you need. But with regex you could solve this in one line. And creating lines of complex if/else trees is prone to bugs.

I would suggest learning to do it the “right” way.

After 2.5 days of slogging through various reg exp blogs/explainers, managed to pass the test. And I think I even understand them a little bit better.

Final code in case anyone is keeping track :slight_smile: :

function telephoneCheck(str) {
  let regexp = /^(1\\s?)?(?:\\d{3}|\\(\\d{3}\\))([-\\/\\.\\s]+)?\\d{3}([-\\/\\.\\s]+)?\\d{4}$/;
   
  return regexp.test(str);
}

We don’t usually post solutions, but if you do, please wrap it in [spoiler]...[/spoiler] tags so people don’t accidentally see the answer if they don’t want to. I’ve done it for you here.

But good work, keep it up. See, regex can be cool. Yes, it’s a pain, but it can be soooooo powerful.

1 Like