MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / mean

Function mean

Maths/AverageMean.js:12–28  ·  view source on GitHub ↗
(numbers)

Source from the content-addressed store, hash-verified

10 * @example mean([10, 40, 100, 20]) = 42.5
11 */
12const mean = (numbers) => {
13 if (!Array.isArray(numbers)) {
14 throw new TypeError('Invalid Input')
15 } else if (numbers.length === 0) {
16 throw new Error('Array is empty')
17 }
18
19 let total = 0
20 numbers.forEach((num) => {
21 if (typeof num !== 'number') {
22 throw new TypeError('Invalid Input')
23 }
24 total += num
25 })
26
27 return total / numbers.length
28}
29
30export { mean }

Callers 2

meanAbsoluteDeviationFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected