[프로그래머스] 모의고사 (javascript)

728x90
반응형
function solution(answers) {
    var answer = [];
    const one=[1,2,3,4,5];
    const two=[2,1,2,3,2,4,2,5];
    const three=[3,3,1,1,2,2,4,4,5,5];
    
    //정답이 같은 것만 골라내서 길이를 구한다
    const result1=answers.filter((answer,idx)=>answer===one[idx%one.length]).length;
    const result2=answers.filter((answer,idx)=>answer===two[idx%two.length]).length;
    const result3=answers.filter((answer,idx)=>answer===three[idx%three.length]).length;
    // console.log(result1,result2,result3)
    const max=Math.max(result1,result2,result3);
    
    if(result1===max){
        answer.push(1);
    }
    if(result2===max){
        answer.push(2);   
    }
    if(result3===max){
        answer.push(3);
    }
    
    return answer;
}

 

728x90
반응형
TAGS.

Comments