電話番号フォーマッタ


コードチャレンジの365日の4日目!
link to the video
免責事項:この問題を解決するために多くの方法がありますこれは私が参照したり、コーディングのインタビューで使用し、適切な答えとして受け入れるだろう答えです
TLDR:解決策はポストの底にある

問題
数字や数字の文字列を受け入れる関数を書き込み、それを米国の電話番号形式に変換します.
例:
     phoneFormat(2359654144) //'235-965-4144'
     phoneFormat('2359654144') //'235-965-4144'
     phoneFormat([2359654144]) //'235-965-4144'
     phoneFormat(1234) //error: was not supplied enough numbers please pass a 10 digit number
     phoneFormat(23596541445) //error: was supplied too many numbers please pass a 10 digit number
     phoneFormat('235$96541445') //error: input must be a number
     phoneFormat([213]) //error: was not supplied enough numbers please pass a 10 digit number
     phoneFormat(null) //error: input must be a number was sent null

解決策
我々がする必要があるものを書きましょう
  • 入力
  • を受け入れる機能を作成します
  • 文字列が文字か文字を含んでいないかどうかを調べます.
  • 文字列
  • に変換しない場合、入力が文字列かどうかを調べます

  • 入力長が10の場合( 10桁の数字)

  • それが
  • はXXX XXX XXXX
  • にフォーマットします

  • を返します.
  • リターンエラーが十分に供給されませんでした10桁の数字
  • を通過してください
    長さが10より大きいかどうかチェックしないならば、
  • リターンはあまりにも多くの数字を供給されてください10桁の数字
  • を通過してください
  • 何かを送信していない場合には、
  • の場合、すべてのキャッチを間違ったエラーになりました
    あなたが詳しい声明に慣れていないならば、さらに行く前にthis MDN pageをチェックしてください.
    まず、我々の機能を作成する必要があります
    const phoneFormat = (input) => {
        //check if the string is a number (doesn’t include characters or letters) and not null or undefined
        //check if the input is a string if not convert it into a string
        //check if the input length is 10 (us number is 10 digits)
            //if it is
                //format it to xxx-xxx-xxxx
                //if not check if it is less than 10
                    //return error was not supplied enough numbers please pass a 10 digit number
                    //if not check if length is greater than 10
                        //return was supplied too many numbers please pass a 10 digit number
                        //if not send something went wrong error for a catch all just in case
    }
    
    文字列が文字か文字を含んでいないかどうかをチェックする必要があります.これを行うにはisnam ()を使用します.NaNは数値ではない.組み込みのJavaScript関数は数字として数字だけの文字列を数えます.あなたがこれに慣れていないならば、続く前に、this MDN pageをチェックしてください.
    また、テンプレートリテラルを使用して、テンプレートリテラルに慣れていない場合は、私たちのエラーをもう少し参考にしてください.
    const phoneFormat = (input) => {
        if(!input || isNaN(input)) return `input must be a number was sent ${input}`
        //check if the input is a string if not convert it into a string
        //check if the input length is 10 (us number is 10 digits)
            //if it is
                //format it to xxx-xxx-xxxx
                //if not check if it is less than 10
                    //return error was not supplied enough numbers please pass a 10 digit number
                    //if not check if length is greater than 10
                        //return was supplied too many numbers please pass a 10 digit number
                        //if not send something went wrong error for a catch all just in case
    }
    
    入力が文字列でなければ、変換を行うことができるように文字列を作成する必要があります.あなたがよく知らないならば.続けてください.
    const phoneFormat = (input) => {
        if(!input || isNaN(input)) return `input must be a number was sent ${input}`
        if(typeof(input) !== 'string') input = input.toString()
        //check if the input length is 10 (us number is 10 digits)
            //if it is
                //format it to xxx-xxx-xxxx
                //if not check if it is less than 10
                    //return error was not supplied enough numbers please pass a 10 digit number
                    //if not check if length is greater than 10
                        //return was supplied too many numbers please pass a 10 digit number
                        //if not send something went wrong error for a catch all just in case
    }
    
    私たちの入力が10桁であるかどうかを確認する必要があります
    const phoneFormat = (input) => {
        if(!input || isNaN(input)) return `input must be a number was sent ${input}`
        if(typeof(input) !== 'string') input = input.toString()
        if(input.length === 10){
            //if it is
                //format it to xxx-xxx-xxxx
                //if not check if it is less than 10
                    //return error was not supplied enough numbers please pass a 10 digit number
                    //if not check if length is greater than 10
                        //return was supplied too many numbers please pass a 10 digit number
                        //if not send something went wrong error for a catch all just in case
        }
    }
    
    入力が10桁であるならば、我々は電話番号にそれをフォーマットする必要があります.あなたがよく知らないならば.replace ()はthis MDN pageを調べてください.あなたがRegexに慣れていないか、よりよく理解したいならば、それはthis MDN pageをチェックしてください.これは、テストと学習Regexのための素晴らしいツールです.
    const phoneFormat = (input) => {
      if(!input || isNaN(input)) return `input must be a number was sent ${input}`
      if(typeof(input) !== 'string') input = input.toString()
      if(input.length === 10){
        return input.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
      } 
      //if not check if it is less than 10
        //return error was not supplied enough numbers please pass a 10 digit number
        //if not check if length is greater than 10
            //return was supplied too many numbers please pass a 10 digit number
            //if not send something went wrong error for a catch all just in case
    }
    
    あなたが私の文字列を各セクション(“$ 1〜$ 2 - 3”)の間のダッシュでフォーマットしたことに気づくでしょう.代わりに、最初の3桁に括弧を付けたいならば、あなたの文字列(“($ 1)$ 2 - 3”)を代わりにする必要があります.
    if if elseステートメントに戻ると、入力が10より小さいかどうかを調べます.もしあなたのエラーが長さに特有であることを望まないならば、あなたが長さが10の形式であるならば、単純なif - else声明を持つことができたならば、さもなければ無効な入力であることに関するメッセージを返してください.しかし、私はより具体的に私のエラーメッセージを作りたかったです、最初に、私は入力が10未満であるかどうかチェックします、そして、それが10桁の数字を供給するよう頼んでいるメッセージを返すならば.私たちは、ここでテンプレートリテラルを可能にして、彼らに彼らが何を供給したかについて知らせさせました、しかし、私は彼らに彼らが十分な数を供給しなかったということを知っているようにすることを決めました.あなたが彼らが彼らがあなたがisnanチェックで送ったメッセージを参照することができる供給をすることを彼らに送りたかったならば.
    const phoneFormat = (input) => {
      if(!input || isNaN(input)) return `input must be a number was sent ${input}`
      if(typeof(input) !== 'string') input = input.toString()
      if(input.length === 10){
        return input.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
      } else if(input.length < 10){
         return 'was not supplied enough numbers please pass a 10 digit number'
      }
        //return error was not supplied enough numbers please pass a 10 digit number
        //if not check if length is greater than 10
            //return was supplied too many numbers please pass a 10 digit number
            //if not send something went wrong error for a catch all just in case
    }
    
    入力が10未満であるならば、入力が10より大きいかどうかチェックするつもりです.何人かの人々はちょうどここで他のものを置いて、10より大きいメッセージを返すかもしれません.しかし、他のインスタンスでは、ロジックチェック以外のものが間違っているのを見ました.たとえば、他のロジックがキャッチしなかった文字列の長さ以外の何かが間違っていたならば、問題がなかったとき、「あまりに多くの数が供給されました」と言うメッセージが10桁の数字を通過してください.多くのことを考えることができないので、この単純な例については、私たちが論理を持っていないのは間違っているでしょうが、そのためには、すべてのエラーをキャッチするために常に良いことがあると思います.
    const phoneFormat = (input) => {
      if(!input || isNaN(input)) return `input must be a number was sent ${input}`
      if(typeof(input) !== 'string') input = input.toString()
      if(input.length === 10){
        return input.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
      } else if(input.length < 10) {
        return 'was not supplied enough numbers please pass a 10 digit number'
      } else if(input.length > 10) {
        return 'was supplied too many numbers please pass a 10 digit number'
      }
      //if not send something went wrong error for a catch all just in case
    }
    
    今必要なのは、すべてのエラーをキャッチし、我々はすべて良いです!
    const phoneFormat = (input) => {
      if(!input || isNaN(input)) return `input must be a number was sent ${input}`
      if(typeof(input) !== 'string') input = input.toString()
      if(input.length === 10){
        return input.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
      } else if(input.length < 10) {
        return 'was not supplied enough numbers please pass a 10 digit number'
      } else if(input.length > 10) {
        return 'was supplied too many numbers please pass a 10 digit number'
      }else{
        return 'something went wrong'
      }
    }
    
    あなたがコメントセクションで思い付いた解決策を残してください.あなたはまた、あなたはそれが来るを参照してください可能性があります以下のコメントでそれを残すことを参照してくださいする必要があります任意の課題がある場合!あなたがチャレンジを取得したい場合は、午前中に毎日通知され、ソリューションが投稿された通知をthis MDN pageを購読する.