奇数を求める


奇数と最小の奇数を求めます

function sol(arr1) {
    let answer=[];
    let oddsum=0, min=Number.MAX_SAFE_INTEGER;
    for(let x of arr1) {
        if(x%2===1){ 
            oddsum += x;
            if(x<min) min=x;
        }
    }

    answer.push(oddsum);
    answer.push(min);
    
    return answer;
}

arr1=[12,77,38,41,53,92,85];
console.log(sol(arr1));  // [256, 41]
function solution(arr) {

    let result=[], min=[], sum = 0;
    
    for(let x of arr) {
        if(x%2===1){
            sum += x;
            min.push(x);
        }
    }

    result.push(sum);
    result.push(Math.min(...min));
    
    return result;
}

let arr = [12, 77, 38, 41, 53, 92, 85];
console.log(solution(arr));  // [256, 41]
⇒ for...of巡視アレイの使用
整列pushで配列に割り当てる