Skip to content

Commit 3d4c407

Browse files
committed
Update mean.js
1 parent 3cb29a0 commit 3d4c407

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

prep/mean.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function calculateMean(list) {
3636
return mean;
3737
}
3838
*/
39+
/*
3940
//const list = [4.6, 5.03, 7.99, 8.01];
4041
function calculateMean(list) {
4142
//1. sum the elements of the array
@@ -57,3 +58,23 @@ console.log(calculateMean([4.6, 5.03, 7.99, 8.01]));
5758
console.log(calculateMean([3, "50", 7]));
5859
5960
//module.exports = calculateMean;
61+
*/
62+
function calculateMean(list) {
63+
//1. sum the elements of the array
64+
let sum = 0;
65+
for (const item of list) {
66+
const value = Number(item);
67+
if (!isNaN(value)) {
68+
sum += value;
69+
}
70+
}
71+
72+
//2. determine the length of the array
73+
let count = list.length;
74+
//3. divide #1 by #2
75+
const mean = sum / count;
76+
//4. return #3
77+
return mean;
78+
}
79+
console.log(calculateMean([4.6, 5.03, 7.99, 8.01]));
80+
console.log(calculateMean([3, "50", 7]));

0 commit comments

Comments
 (0)