로또의 최고 순위와 최저 순위

Clare Lee·2021년 8월 18일
0

코딩 문제

목록 보기
1/1
post-thumbnail

예전에 풀었던 문제였는데 처음부터 다시 풀려고 열어봤다.

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]];
}
profile
2년차 임베디드 SW 개발자

0개의 댓글