-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalAverage.js
More file actions
40 lines (29 loc) · 753 Bytes
/
calAverage.js
File metadata and controls
40 lines (29 loc) · 753 Bytes
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
/**
* -------------------------------------------------------
* Programming Question : Calculate the Average
* -------------------------------------------------------
**/
// Q. Write a function called calculateAverage that takes ans array of numbers as input and returns the average of those numbers.
//constraint
//?
//?
//?
//?
function calculateAverage(arr) {
//using reduce
let total= arr.reduce((accm,curr) => accm+curr,0);
return total/arr.length
// using for in
let sum =0;
for(let ar in arr){
sum = sum + arr[ar];
}
return sum / arr.length;
// using for in
let sum =0;
for(let ar of arr){
sum += ar
}
return sum / arr.length;
}
console.log(calculateAverage([5,10,2,8]));