본문 바로가기

Algorithm/문제풀이

Programmers - 모의고사 / Javascript

전날 아무리 해도 자꾸 테스트 케이스 검증에서 50점만 맞다가 오늘 아침에 일어나서 코드 싹지우고 다시해보니까 통과!

이렇게 간단한 로직인데 어제는 왜 안됐던건지 모르겠다 후 분명 동일한 로직에 코드만 약간 달랐던 것 같은데...

이전 코드를 비교해볼수가 없어서 아쉽긴하지만 어제의 자괴감에서 탈출할 수 있어서 다행이다.

function solution(answers) {
    const scores = [0, 0, 0];
    const a = [1, 2, 3, 4, 5];
    const b = [2, 1, 2, 3, 2, 4, 2, 5];
    const c = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5,];
    
    answers.map((answer, i) => {
        if(a[i % a.length] === answer) scores[0]++;
        if(b[i % b.length] === answer) scores[1]++;
        if(c[i % c.length] === answer) scores[2]++;
    })
    
    const maxScore = Math.max(...scores);
    const result = [];
    
    scores.map((score, i)=> {
        if(score === maxScore) result.push(i+1);
    }); 
    
    return result;
}