코딩 테스트/test_javaScript

[코딩테스트/javaScript] 모의고사

ijooha 2025. 1. 16. 16:07

function solution(answers) {
    const firstP = [1,2,3,4,5];
    const secondP = [2,1,2,3,2,4,2,5];
    const thirdP = [3,3,1,1,2,2,4,4,5,5];
    
    let score = [0,0,0]
    
    for (let i=0; i<answers.length; i++) {
        if (answers[i] === firstP[i%5]) {score[0]++}
        if (answers[i] === secondP[i%8]) {score[1]++}
        if (answers[i] === thirdP[i%10]) {score[2]++}
    }
    let scoreMax = Math.max(...score);
    let answer = [];
    
    score.forEach((char, idx) => {
        if (char === scoreMax) {
            answer.push(idx+1);
        }
    })
    return answer;
}

정말 복잡 구구절절한 문제...