function solution(answers) {
const pattern1 = [1,2,3,4,5]
const pattern2 = [2, 1, 2, 3, 2, 4, 2, 5]
const pattern3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
const pattern = [pattern1, pattern2, pattern3]
const score = [0, 0, 0]
// answers를 각 패턴에 따라 같은 답이 있으면 각 점수를 추가
answers.forEach((ele, idx)=>{
for(let i = 0; i<pattern.length; i++){
ele === pattern[i][idx%pattern[i].length] ? score[i] += 1 : score[i]
}
})
// 점수를 비교
const [first, two, three] = score
const max = Math.max(first, two, three)
return score.map((ele, idx)=>{
if(ele === max){
return idx+1;
}
}).filter((ele) => ele)
}
if(ele === pattern[0][idx%pattern[0].length]) score[0] += 1
if(ele === pattern[1][idx%pattern[1].length]) score[1] += 1
if(ele === pattern[2][idx%pattern[2].length]) score[2] += 1
이렇게 하드코딩하는 편이 더 좋았을지 모르겠다. 반복문을 굳이 돌릴 필요가 있었을까?
const result = []
...
for(let i = 0; i<score.length; i++){
if(max === score[i]) result.push(i+1);
}
해당 식을 쓰는게 훨씬 더 깔끔하고 보기 좋다는 걸 뒤늦게 생각
너무 어려운 길로 돌아갔다
function solution(answers) {
var answer = [];
const man1 = [1, 2, 3, 4, 5];
const man2 = [2, 1, 2, 3, 2, 4, 2, 5];
const man3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
let count = [0, 0, 0];
for(let i = 0; i < answers.length; i++) {
if(answers[i] == man1[i % man1.length]) count[0]++;
if(answers[i] == man2[i % man2.length]) count[1]++;
if(answers[i] == man3[i % man3.length]) count[2]++;
}
const max = Math.max(count[0], count[1], count[2]);
for(let i = 0; i < count.length; i++) {
if(max == count[i]) answer.push(i + 1);
}
return answer;
}