Lv1. 모의고사 Javascript
https://programmers.co.kr/learn/courses/30/lessons/42840
function solution(ans) {
let answer = [];
const sp1 = [1, 2, 3, 4, 5];
const sp2 = [2, 1, 2, 3, 2, 4, 2, 5];
const sp3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
const count = [0, 0, 0];
for (let i = 0; i < ans.length; i++) {
if (ans[i] === sp1[i % sp1.length]) {
count[0] += 1;
}
if (ans[i] === sp2[i % sp2.length]) {
count[1] += 1;
}
if (ans[i] === sp3[i % sp3.length]) {
count[2] += 1;
}
}
const maxVal = Math.max.apply(null, count);
for (let i = 0; i < count.length; i++) {
if (maxVal === count[i]) answer.push(i + 1);
}
return answer;
}
filter 활용법
➤
댓글 환영
질문 환영
by.protect-me