Given 7 natural numbers, write a program to select all odd natural numbers, find the sum, and find the smallest of the odd numbers.
For example, given 7 natural numbers 12, 77, 38, 41, 53, 92, 85, the odd numbers among them are 77, 41, 53, 85, so the sum is
77 + 41 + 53 + 85 = 256
41 < 53 < 77 < 85
Therefore, the minimum value among odd numbers is 41.
The first line gives 7 natural numbers. The given natural number is less than 100. There must be at least one odd number.
12 77 38 41 53 92 85
The first line prints the sum of odd numbers, and the second line prints the minimum among odd numbers.
256
41
for
onlyfunction solution(n) {
let answer = [];
let sum = 0,
min = Number.MAX_SAFE_INTEGER;
for (let x of arr) {
if (x % 2 === 1) {
sum += x;
if (x < min) min = x;
}
}
answer.push(sum);
answer.push(min);
return answer;
}
reducer()
function solution(arr) {
let answer = [];
let odd = [];
for (n of arr) {
if (n % 2 !== 0) odd.push(n);
}
let sum = odd.reduce((sum, cur) => sum + cur, 0);
let min = Math.min(...odd);
answer.push(sum);
answer.push(min);
return answer;
}