개발/코딩테스트

프로그래머스 "모의고사", JavaScript 풀이

꾸럭 2021. 4. 29. 17:00
function solution(answers) {
    const human1 = [1, 2, 3, 4, 5];
    const human2 = [2, 1, 2, 3, 2, 4, 2, 5];
    const human3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

    let pointAry = Array(3).fill(0);
    answers.forEach((answer, index) => {
        const human1Index = useIndex(human1, index);
        const human2Index = useIndex(human2, index);
        const human3Index = useIndex(human3, index);

        if (human1[human1Index] === answer) {
            pointAry[0]++;
        }
        if (human2[human2Index] === answer) {
            pointAry[1]++;
        }
        if (human3[human3Index] === answer) {
            pointAry[2]++;
        }
    });

    const maxPoint = Math.max(...pointAry);
    return pointAry.reduce((ary, point, index) => {
        if (point === maxPoint) {
            ary.push(index + 1);
        }

        return ary;
    }, []);
}

function useIndex(ary, index) {
    return index < ary.length ? index : index % ary.length;
}
728x90

'개발 > 코딩테스트' 카테고리의 다른 글

프로그래머스 "H-Index", JavaScript 풀이  (0) 2021.04.29