-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
get-the-mean-of-an-array.js
60 lines (51 loc) · 1.32 KB
/
get-the-mean-of-an-array.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function getAverage(marks) {
// a place to store the sum, initialized to zero
let sum = 0;
// iterate over the marks
for (let i = 0; i < marks.length; i += 1) {
// add the current mark to the sum
const mark = marks[i];
// sum += mark;
sum = sum + mark;
}
// return the sum divided by the number of marks
return Math.floor(sum / marks.length);
}
function getAverage(marks) {
let sum = 0;
for (let i = 0; i < marks.length; i += 1) {
const mark = marks[i];
sum = sum + mark;
}
return Math.floor(sum / marks.length);
}
function getAverage(marks) {
let sum = 0;
marks.forEach((mark) => {
sum += mark;
});
return Math.floor(sum / marks.length);
}
function getAverage(marks) {
const sum = marks.reduce((sum, mark) => {
return sum + mark;
}, 0);
return Math.floor(sum / marks.length);
}
function getAverage(marks) {
return Math.floor(marks.reduce((sum, mark) => {
return sum + mark;
}, 0) / marks.length);
}
function getAverage(marks) {
return Math.floor(marks.reduce((sum, mark) => sum + mark) / marks.length);
}
function sumArray(arr, sum) {
if (arr.length === 0) {
return sum;
}
return sum + arr[0] + sumArray(arr.slice(1), sum);
}
console.log(getAverage([2,2,2,2]), 2);
console.log(getAverage([1,2,3,4,5]), 3);
console.log(getAverage([1,1,1,1,1,1,1,2]), 1);