How to Calculate the Product of Numeric Values in JavaScript
To calculate the product of two or more numbers or arrays in JavaScript, follow these steps:
- Open the Terminal/SSH and type
node
to start practicing coding.
- Use the
Array.prototype.reduce()
method to multiply each value with an accumulator, which should be initialized with a value of 1
.
- Define a function called
prod
that takes any number of arguments using the spread operator (...
). Within the function, apply the reduce()
method to the spread array of arguments.
- The function returns the result of the multiplication.
Here's an example of how to use the prod
function:
const prod = (...arr) => [...arr].reduce((acc, val) => acc * val, 1);
prod(1, 2, 3, 4); // 24
prod(...[1, 2, 3, 4]); // 24
In the above example, prod(1, 2, 3, 4)
and prod(...[1, 2, 3, 4])
both return 24
.