mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 07:29:47 +08:00

* docs: fixed misleading comment about the array method (forEach instead of reduce) used in AverageMean.js * fix: optimized AverageMean.js by removing redundant comments and unnecessary operations. * Update Maths/AverageMean.js Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com> --------- Co-authored-by: Hridyanshu7 <himank7794@gmail.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
20 lines
498 B
JavaScript
20 lines
498 B
JavaScript
/**
|
|
* @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
|
|
*/
|
|
|
|
const mean = (nums) => {
|
|
if (!Array.isArray(nums)) {
|
|
throw new TypeError('Invalid Input')
|
|
}
|
|
|
|
return nums.reduce((sum, cur) => sum + cur, 0) / nums.length
|
|
}
|
|
|
|
export { mean }
|