forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAverageMean.js
More file actions
Latest commit
23 lines (19 loc) · 638 Bytes
/
Copy pathAverageMean.js
File metadata and controls
23 lines (19 loc) · 638 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* @function mean
* @description This script will find the mean value of a array of numbers.
* @param {Integer[]} nums - Array of integer
* @return {Integer} - mean of nums.
* @see [Mean](https://en.wikipedia.org/wiki/Mean)
* @example mean([1, 2, 4, 5]) = 3
* @example mean([10, 40, 100, 20]) = 42.5
*/
constmean=(nums)=>{
if(!Array.isArray(nums)){
thrownewTypeError('Invalid Input')
}
// This loop sums all values in the 'nums' array using forEach loop
constsum=nums.reduce((sum,cur)=>sum+cur,0)
// Divide sum by the length of the 'nums' array.
returnsum/nums.length
}
export{mean}