mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00

The URL in the Wikipedia link had an extra "h" in the URI Scheme (the part that specifies the protocol, usually `https`). I removed the duplicate "h" and made it into a single "h", now the link works
24 lines
638 B
JavaScript
24 lines
638 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')
|
|
}
|
|
|
|
// This loop sums all values in the 'nums' array using forEach loop
|
|
const sum = nums.reduce((sum, cur) => sum + cur, 0)
|
|
|
|
// Divide sum by the length of the 'nums' array.
|
|
return sum / nums.length
|
|
}
|
|
|
|
export { mean }
|