sum
math
Computes the sum of the values in array.
Installation
Import
import { sum } from '@tulx/utils';Source Code
Implementation
/**
* Computes the sum of the values in array.
*
* @param array - The array to iterate over.
* @returns Returns the sum.
*
* @example
* ```ts
* sum([4, 2, 8, 6]); // 20
* ```
*/
export function sum(array: readonly number[]): number {
return array.reduce((acc, val) => acc + val, 0);
}
Example
import { sum } from '@tulx/utils';
sum([4, 2, 8, 6]); // 20Related 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.