文字列処理ベース(プログラマ)



問題の説明

  • 文字列sの長さが4または6であり、数値のみからなる関数を作成します.
  • の数字のみからなる場合はtrue、false
  • です.

    せいげんじょうけん

  • sは、長さが1または8未満の文字列
  • である.

    に答える

    function solution(s) {
        let answer = true;
        
        if(s.length === 4 || s.length === 6){
            const sArr = s.split('');
            sArr.forEach(s=>{
                if(s.charCodeAt(0)<48 || s.charCodeAt(0)>58)
                    return answer = false;
            })
        }
        else
            return false;
        
        return answer;
    }

    チェックポイント

  • String.prototype.charCodeAt():文字のUnicode位置
  • String.prototype.charat():Unicode位置の文字