simple-statistics
Version:
28 lines (25 loc) • 628 B
JavaScript
/**
* This computes the maximum number in an array.
*
* This runs in `O(n)`, linear time, with respect to the length of the array.
*
* @param {Array<number>} x sample of one or more data points
* @returns {number} maximum value
* @throws {Error} if the length of x is less than one
* @example
* max([1, 2, 3, 4]);
* // => 4
*/
function max(x) {
if (x.length === 0) {
throw new Error("max requires at least one data point");
}
let value = x[0];
for (let i = 1; i < x.length; i++) {
if (x[i] > value) {
value = x[i];
}
}
return value;
}
export default max;