-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay21.js
More file actions
25 lines (22 loc) · 946 Bytes
/
Day21.js
File metadata and controls
25 lines (22 loc) · 946 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
// Coding Challenge
// Write a function called calculateMean that takes an array of numbers as input and returns the mean (average) of those numbers.
// In math, the mean is the average of a set of numbers, or the numeric value that represents the center of a collection of numbers.
// Constraints:
// - The input array may contain positive and negative integers.
// - The input array may be empty. If it is empty, the function should return 0.
const calculateMean = (arr) => {
if (arr.length === 0) {
return 0;
}
// let sum = 0;
// for (let num of arr) {
// sum += num;
// }
let sum = arr.reduce((acum,curElem) => acum + curElem,0);
console.log(sum);
return sum / arr.length;
}
console.log(calculateMean([1, 2, 3, 4, 5])); // Output: 3
console.log(calculateMean([10, 20, 30])); // Output: 20
console.log(calculateMean([-1, 0, 1])); // Output: 0
console.log(calculateMean([])); // Output: 0