수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다.
1번 수포자가 찍는 방식: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2번 수포자가 찍는 방식: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3번 수포자가 찍는 방식: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...
1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return 하도록 solution 함수를 작성해주세요.
answers | return |
---|---|
[1,2,3,4,5] | [1] |
[1,3,2,4,2] | [1,2,3] |
function solution(answers) {
var answer = [];
const student = {
1 : [1,2,3,4,5],
2 : [2,1,2,3,2,4,2,5],
3 : [3,3,1,1,2,2,4,4,5,5]
}
let count = {
1 : 0,
2 : 0,
3 : 0
}
for(let index = 0 ; index < answers.length ; index++){
for(let num = 1 ; num <= 3 ; num++){
if(student[num][index >= student[num].length ? index % student[num].length: index]
=== answers[index])
count[num]++;
}
}
if(count[1] >= count[2] && count[1] >= count[3]){ answer.push(1); }
if(count[2] >= count[1] && count[2] >= count[3]){ answer.push(2); }
if(count[3] >= count[1] && count[3] >= count[2]){ answer.push(3); }
return answer;
}