강의

멘토링

커뮤니티

Inflearn Community Q&A

rlawlsdn2632296's profile image
rlawlsdn2632296

asked

Introduction to Javascript Algorithm Problem Solving (Coding Test Preparation)

6. Odd

forEach와 Math.min()을 사용해봤어요

Written on

·

250

0

저는 forEach를 사용해 배열을 순환하고 따로 홀수들만 들어있는 배열을 만들어 Math.min()을 사용해서 결과값을 내봤어요. 좋은 답은 아니라고 생각했지만 우선 결과값을 내고 싶어서 이렇게 해봤어요ㅠ

let arr = [12, 77, 38, 41, 53, 92, 85];

function solution(arr) {
    let answer = [];
    let odd_sum = 0, odd_array = [];
    arr.forEach(item => {
        if(item % 2 !== 0) {
            odd_array.push(item);
            odd_sum += item;
        }
    })
    answer.push(odd_sum);
    answer.push(Math.min(...odd_array));
    return answer;
}

solution(arr);
foreachjavascriptMath.min코테 준비 같이 해요! 홀수

Answer

This question is waiting for answers
Be the first to answer!
rlawlsdn2632296's profile image
rlawlsdn2632296

asked

Ask a question