UNPKG

565 BJavaScriptView Raw
1/**
2 * The [product](https://en.wikipedia.org/wiki/Product_(mathematics)) of an array
3 * is the result of multiplying all numbers together, starting using one as the multiplicative identity.
4 *
5 * This runs in `O(n)`, linear time, with respect to the length of the array.
6 *
7 * @param {Array<number>} x input
8 * @return {number} product of all input numbers
9 * @example
10 * product([1, 2, 3, 4]); // => 24
11 */
12function product(x) {
13 let value = 1;
14 for (let i = 0; i < x.length; i++) {
15 value *= x[i];
16 }
17 return value;
18}
19
20export default product;