2021-05-11 13:34:23 +02:00
|
|
|
/**
|
|
|
|
* Returns the sum of all values in an array.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* Prints 6 for the array [1, 2, 3]:
|
|
|
|
* ```typescript
|
|
|
|
* console.log(arraySum([1, 2, 3]));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param arr - An array of numbers.
|
|
|
|
* @return The sum of all values in the array.
|
|
|
|
*/
|
2021-04-29 19:10:39 +02:00
|
|
|
function arraySum(arr: Array<number>): number {
|
|
|
|
return arr.reduce((accumulator, current) => accumulator + current, 0);
|
2020-12-05 07:29:59 +01:00
|
|
|
}
|
2021-04-29 19:10:39 +02:00
|
|
|
|
2021-05-11 19:41:47 +02:00
|
|
|
|
2021-05-11 13:34:23 +02:00
|
|
|
/** @exports */
|
2021-05-10 18:15:25 +02:00
|
|
|
export { arraySum };
|