mean
math
Computes the mean of the values in array.
Installation
Import
import { mean } from '@tulx/utils';Source Code
Implementation
/**
* Computes the mean of the values in array.
*
* @param array - The array to iterate over.
* @returns Returns the mean.
*
* @example
* ```ts
* mean([4, 2, 8, 6]); // 5
* ```
*/
export function mean(array: readonly number[]): number {
if (array.length === 0) {
return NaN;
}
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
return sum / array.length;
}
Example
import { mean } from '@tulx/utils';
mean([4, 2, 8, 6]); // 5Related Functions
add
Adds two numbers.
ceil
Computes number rounded up to precision.
divide
Divide two numbers.
floor
Computes number rounded down to precision.
max
Computes the maximum value of array.
maxBy
This method is like max except that it accepts iteratee which is invoked for each element in array to generate the criterion by which the value is ranked.