Write a javascript program that takes a numbers array and returns the product of all the numbers.
Write a javascript program that takes a numbers array and returns the product of all the numbers.
334
14-Apr-2023
Updated on 26-Nov-2023
Aryan Kumar
26-Nov-2023Certainly! You can create a JavaScript program that calculates the product of all the numbers in an array by using the reduce method. Here's an example:
In this example, the calculateProduct function takes an array of numbers and uses the reduce method to multiply them together. The reduce function takes a callback function with an accumulator (initialized to 1) and the current element of the array. The accumulator accumulates the product as the function iterates through the array.
The example usage demonstrates how to use the function with a sample array ([2, 3, 5, 7]). The result is then printed to the console.