정수가 담긴 리스트 num_list가 주어질 때, num_list의 원소 중 짝수와 홀수의 개수를 담은 배열을 return 하도록 solution 함수를 완성해보세요.
|num_list| result|
|:---:|:--:|
|[1, 2, 3, 4, 5]| [2, 3]|
|[1, 3, 5, 7]| [0, 4]|
function solution(num_list) {
return [
num_list.filter((num) => num % 2 === 0).length,
num_list.filter((num) => num % 2 === 1).length,
];
}
//1
function solution(num_list) {
const evenLength = num_list.filter(n => n % 2 === 0).length;
return [evenLength, num_list.length - evenLength];
}
//2 짝수 홀수의 나머지를 인덱스로 사용 for ... of 반복문 사용
function solution(num_list) {
var answer = [0,0];
for(let a of num_list){
answer[a%2] += 1
}
return answer;
}