2021 KAKAO BLIND RECRUITMENT-신규 아이디 추천

05.25에 푼 문제입니다🌷
신규아이디추천

function solution(new_id) {
    var answer = '';
    var nonText = ['-','_','.']
    //1. 소문자로 변경
    new_id=new_id.toLowerCase()
    
    // 2. 문자 확인
    new_id=[...new_id]
    new_id.map((a,i)=>{
        if(!nonText.includes(a)){
            if(!isAplhaMath(a)){
                new_id[i]=''
            }
        }
    })
    new_id=[...new_id.join('')]
    //3. . 지우기
    var pointCheck=false
    new_id.map((a,i)=>{
        if(a==='.'){
            if(pointCheck) new_id[i]=''
            pointCheck=true
        }
        else pointCheck=false
    })
    new_id=[...new_id.join('')]
    //4.
    if(new_id[0]==='.') new_id[0]=''
    if(new_id[new_id.length-1]==='.')
        new_id[new_id.length-1]=''
    new_id=[...new_id.join('')]
    //5
    if(new_id.length===0){
        new_id[0]='a'
    }
    new_id=new_id.join('')
    //6
    if(new_id.length>15) new_id=new_id.slice(0,15)
    if(new_id[14]==='.') new_id=new_id.slice(0,14)
    
    //7
    if(new_id.length<3) {
        for(let i= new_id.length;i<3;i++) new_id+=new_id[new_id.length-1]
    }
    return new_id;
}

function isAplhaMath(a){
    var check=/^[a-z0-9]+$/;
    return (check.test(a))?true:false
}

정규표현식을 알면 더 간결하게 풀 것 같다!

좋은 웹페이지 즐겨찾기