[프로그래머스] Lv. 0 - 짝수 홀수 개수

지윤·2023년 1월 22일
0

프로그래머스

목록 보기
18/69

문제

풀이

function solution(num_list) {
    var answer = [];
    let evenCnt = 0;
    let oddCnt = 0;
    
    num_list.forEach(e => {e % 2 == 0 && evenCnt++});
    num_list.forEach(e => {e % 2 == 1 && oddCnt++});
    answer.push(evenCnt,oddCnt);
    
    return answer;
}

forEach를 사용해서 짝수 일때는 evenCnt를 증가시켜줬고, 홀수 일때는 oddCnt를 증가 시켜줬다. forEach는 map처럼 각 요소에 대해 callback을 싱행하지만 map은 실행결과를 모은 새 배열을 return 해준다!

profile
방금 태어난 개발자

0개의 댓글