교육/코테

[프로그래머스]JS 모의고사

가이버2 2022. 5. 20. 10:39

코딩테스트 연습 - 모의고사 | 프로그래머스 (programmers.co.kr)

변수명 제대로 answers, answer

 

코딩테스트 연습 - 모의고사

수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다. 1번 수포자가 찍는

programmers.co.kr

function solution(answers) {
    let answer = [];
    const first = [1,2,3,4,5];
    const second = [2,1,2,3,2,4,2,5];
    const third = [3,1,2,4,5];
    let correctCount = [0, 0, 0];
    
    for(let i=0; i<answers.length; i++){
        // 1. n        
        if((first[i%5]) == answers[i]) correctCount[0]++;
        // 2. 2,(1,3,4,5)
        if(second[i%8] == answers[i]) correctCount[1]++;
        // 3. 31245 * 2        
        if(third[parseInt(i/2)%5] == answers[i]) correctCount[2]++;
    }

    let highScore = 0;
    for(let i=0; i<correctCount.length; i++){
        if(highScore < correctCount[i]) highScore = correctCount[i];
    }
    for(let i=0; i<correctCount.length; i++){
        if(highScore==correctCount[i]) answer.push(i+1);
    }
    return answer;
}