예전에 풀었던 문제였는데 처음부터 다시 풀려고 열어봤다.
function solution(lottos, win_nums) {
let result = [6, 6, 5, 4, 3, 2, 1];
let matchNumber = lottos.filter(lotto => win_nums.includes(lotto)).length;
let numZeros = lottos.filter(lotto => lotto === 0).length;
return [result[matchNumber+numZeros], result[matchNumber]]
}
예전에는 위와 같이 풀었는데, 이번에는 큰 차이 없는 코드를 짰다. 다만 문제를 풀고 이전에 푼 코드를 봤을 때 let을 쓴 것에 좀 경악했었다. 작은 부분이지만 생각보다 코딩하면서 발전하는구나라고 느꼈다.
function solution(lottos, win_nums) {
const win_array = [6, 6, 5, 4, 3, 2, 1]
const existing_total = lottos.filter(num => win_nums.includes(num)).length;
const maybe_total = lottos.filter(num => num === 0).length;
return [win_array[existing_total + maybe_total], win_array[existing_total]];
}