JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

Tell us what’s happening:
Describe your issue in detail here.

Disclaimer: Yes, I know this is long and could potentially be made into a one-liner. However, in order to feel like I am actually completing this certification with my own work and without relying on other’s work I went about it this way in order to be able to start something on my own. I may rework it later into a one-liner, but I want to make sure this one works first before I do that (unless if statements in this manner just straight up don’t work – in that case do let me know).

The problem: I can’t seem to get the country code part (the eleven digit parts of the code) or the parentheses and spacing to read properly. I tried to give conditions that made it so that the digits would appear in certain groupings with the spaces and hyphens in between, but it seems like something went wrong.

Currently the console results read as:
// running tests
telephoneCheck(“1 555-555-5555”) should return true.
telephoneCheck(“1 (555) 555-5555”) should return true.
telephoneCheck(“555-555-5555”) should return true.
telephoneCheck(“(555)555-5555”) should return true.
telephoneCheck(“1(555)555-5555”) should return true.
telephoneCheck(“1 555 555 5555”) should return true.
telephoneCheck(“1 456 789 4444”) should return true.
telephoneCheck(“(6054756961)”) should return false.
telephoneCheck(“27576227382”) should return false.
// tests completed

Your code so far

function telephoneCheck(str) {
const wordBouncer = /[\d"("")"" ""-"]/;
const hasSpace = /\s+/;
const hasHyphen = /"-"+/;
const tenDigitNum = /\d{10}/;
const tenParentheses = /\(\d{3}\)/;
const tenParenSpace = /^\(\d{3}\)\s\d{3}\s\d{4}$/;
const tenParenHyphen = /[^(\d{3})\d{3}"-"\d{4}$]|[^\(\d{3}\)\s\d{3}"-"\d{4}$]/;
const tenRegSpace = /^\d{3}\s\d{3}\s\d{4}$/;
const tenRegHyphen = /^\d{3}"-"\d{3}"-"\d{4}$/;
const elevenDigitNum = /\d{11}/;
const elevenParentheses = /[1\(\d{3}\)]|[1\s\(\d{3}\)]/;
const elevenParenSpace = /[^1\(\d{3}\)\d{3}\s\d{4}$]|[^1\s\(\d{3}\)\s\d{3}\s\d{4}$]/;
const elevenRegSpace = /^1\s\d{3}\s\d{3}\s\d{4}$/;
const elevenRegHyphen = /[^1"-"\d{3}\d{3}"-"\d{4}$]|[^1\s\d{3}"-"\d{3}"-"\d{4}$]/;

let wordBouncerStart = wordBouncer.test(str);
let spaceCheck = hasSpace.test(str);
let hyphenCheck = hasHyphen.test(str);
let checkTen = tenDigitNum.test(str);
let tenParenCheck = tenParentheses.test(str);
let tenParenSpaceCheck = tenParenSpace.test(str);
let tenParenHyphenCheck = tenParenHyphen.test(str);
let tenRegSpaceCheck = tenRegSpace.test(str);
let tenRegHyphenCheck = tenRegHyphen.test(str);
let checkEleven = elevenDigitNum.test(str);
let elevenParenCheck = elevenParentheses.test(str);
let elevenParenSpaceCheck = elevenParenSpace.test(str);
let elevenRegSpaceCheck = elevenRegSpace.test(str);
let elevenRegHyphenCheck = elevenRegHyphen.test(str);
if (wordBouncerStart === false) {
  return false;
}
if (checkTen === true) {
  if (tenParenCheck === true) {
    if (tenParenHyphenCheck === true || tenParenSpaceCheck === true) {
      return true;
    } else {
      return false;
    }
  }
  if (tenParenCheck === false) {
    if (tenRegSpaceCheck === true || tenRegHyphenCheck === true) {
      return true;
    } else {
      if (spaceCheck === false && hyphenCheck === false) {
        return true;
      } else {
        return false;
      }
    }
  }
}
if (checkEleven === true) {
  if (elevenParenCheck === true) {
    if (elevenParenSpaceCheck === true || elevenParenHyphenCheck === true) {
      return true;
    } else {
      return false;
    }
  }
  if (elevenParenCheck === false) {
    if (elevenRegSpaceCheck === true ||elevenRegHyphenCheck === true) {
      return true;
    } else {
      if (spaceCheck === false && hyphenCheck === false) {
        return true;
      } else {
        return false;
      }
    }
  }
} 
if (checkEleven === false && checkTen === false) {
  return false;
}
}

telephoneCheck("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/116.0.0.0 Safari/537.36 Edg/116.0.1938.81

Challenge: JavaScript Algorithms and Data Structures Projects - Telephone Number Validator

Link to the challenge:

I’m going to be honest with you here, this code is very hard to read and make sense of. You might be able to do it since you are very familiar with it, but I’m guessing most people are going to quit trying to understand it rather quickly.

I understand you want to do it “your way”, but unfortunately your way is way too convoluted. Yes, there is a solution for this that basically involves one regular expression. The “trick” to it is to understand how to use some features of regular expressions that you might not be 100% comfortable with. But that’s how you learn. Does this mean your solution will basically be the same as everyone else’s? Yes, but there is probably a reason for that. Sometimes a solution is popular because it is the best way to solve a problem.

So I would encourage you to re-work this into a “one-liner” now. But perhaps someone else here will take the time to decipher your code and find your issue?

Good luck.

Yeah… I realize now I was being a bit too stubborn. It was much faster and easier to do the shorter version (though for me at least the trying to work through the longer version did help me understand regex more fully so that’s a bit of a silver lining I guess). Having more steps on the other one probably made it where there was more room for error. If anyone is willing to still look through this longer one to see where I went wrong (other than I made it a lot more complicated than it had to be), it would be appreciated.

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