Validate US Telephone Numbers

Hi guys.

Visit Challenge

I solved front end algorithm challenge to validate US telephone numbers. I enjoyed the challenge and completed it using regex and functions to satisfy all the test cases. In the end I am not very satisfied. Is the goal to build a good solution or is it enough if the solutions passes the test cases. I am asking this for all the algorithm challenges in general.

If you completed it, you completed it. As much as I’d love to encourage people to hone their programming skills by perfecting each of the algorithm challenges, it’s a pretty silly thing to do unless its 1) unreadable or 2) performing badly. I think the natural impulse is to try to solve the challenge code golf style - that is, using as few written instructions as possible - and if you’ve got a few more lines than another person, your own solution is somehow suboptimal. It’s not a bad thing, and if you’ve got the time and the desire, go ahead and re-write everything you do to be as few lines as possible. Just know that doesn’t make things necessarily better.

All that said, what is it about your solution that you don’t like?

Here is my solution. I am not comfortable with part where it checks for -1 or braces at ends.

function telephoneCheck(str) {
// Good luck!

    function numberStrip(number) {
      
    return number.replace(/[-() ]/g,"");  

    }
  
    
     function validatePhone(number) {
      
      if (sanityCheck(number)) {
     
          var phoneNumber = numberStrip(number).split("");

          if(phoneNumber.length ==11)
          {
              if(phoneNumber[0] == 1 ) {

                return true;

              }

              else {

                return false;

              }
          }
          else if(phoneNumber.length == 10) {

            return true;

          } else {

            return false;

          }  
        
      } else {
        
        
        return false;
        
      }
      

    }
  
    
    function bracketCheck(number) {

        var phoneNumber = number;

        if((phoneNumber.indexOf('(') !== -1 && phoneNumber.indexOf(')') !== -1) || (phoneNumber.indexOf('(') === -1 &&             phoneNumber.indexOf(')') === -1))

         return true;
      
        else {
          return false;
          
        }

    }
  
    function splCharTest(number) {


    var x = /[^0-9-() ]/.test(number);

    return x;

    }
  
  function firstDigitCheck(number){
  
  var stringNum = number.split("");
    
  
  if(stringNum[0] !== "-" && stringNum[stringNum.length -1] !== ")" ){
    
    return true;
    
  } else {
    
    
    return false;
    
  }
    
    
    
    
    
  }
  
  function sanityCheck(number) {
        
        if (firstDigitCheck(number) === false) {
          
          return false;
        }
        else if(splCharTest(number) === true){

            return false;

        } else if(!bracketCheck(str)) {

            return false;

        } else {

          return true;

        }
    
    }  
  
   
   if(validatePhone(str)) {
     
     return true;
     
   } else {
    
    return false;
    
  } 
  
 

  
     
}


telephoneCheck("123**&!!asdf#");

Hi! I would recommend you to take a look at this topic: http://forum.freecodecamp.com/t/how-close-my-solutions-of-the-algorithm-scriptings-should-match-the-wiki/4283