-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavg-of-odd.js
More file actions
31 lines (28 loc) · 1.09 KB
/
avg-of-odd.js
File metadata and controls
31 lines (28 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// function takes an array as parameter
// give me the average of the odd numbers in the array
// put odd numbers in an array
function odd_avg (numbers) {
// console.log(numbers);
let odds = [];
for (const number of numbers) {
// console.log(number);
if (number % 2 === 1) {
// console.log(number);
odds.push(number)
}
}
// odds is the array that contain only the odd numbers
// console.log(odds);
let sum = 0;
for (const number of odds) {
sum = sum + number;
}
// console.log(sum);
let length = odds.length;
console.log("sum of odd numbers is ", sum, "and length of odd numbers array is", length);
let avg = sum / length;
return avg;
}
let numbers = [12, 3, 45, 44, 32, 77, 40, 80, 99, 105, 203, 11];
let avg = odd_avg (numbers);
console.log("average of odd numbers is: ", avg);