Open In App

Multiply the elements of an array in JavaScript

Last Updated : 05 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We will be given an initial array and we have to multiply all the elements of the array with each other to find a final product.

Examples:

Input: arr = [1, 2, 3, 4, 5];
Output: 120
Explanation: 1 * 2 * 3 * 4 * 5 = 120

Input: arr = [11, 12, 13, 14, 15]
Output: 360360
Explanation: 11 * 12 * 13 * 14 * 15 = 360360

There are three ways available in which we can solve this problem in JavaScript:

Recursive Method:

In this approach, we will recursively visit each element of the array and multiply it with the product of all the previous elements to get a new product value till the current element.

Example: The below code example will explain to you the practical implementation of the recursive approach to solve this problem in JavaScript:

Javascript




const arr1 = [8, 9, 3, 7, 5, 13];
const arr2 = [12, 9, 5, 18, 23];
 
const recursiveMultiply = (arr, ind) => {
    if (ind === arr.length - 1) {
        return (arr[ind]);
    }
    else {
        return (arr[ind] * recursiveMultiply(arr, ind + 1))
    }
}
 
console.log(recursiveMultiply(arr1, 0));
console.log(recursiveMultiply(arr2, 0));


Output

98280
223560

Time Complexity: O(n), n is the size of the passing array.

Space Complexity: O(n), n is the size of the passing array.

Iterative Method:

In this method, we will use a simple for loop in JavaScript to iterate through the elements of the array and multiply them with each other to get the multiply result at the end. This approach is more efficient than the previous recursive approach as it takes the linear time complexity and constant extra space to solve the probelm.

Example: The below example will illustrate how you can multiply elements of an array using a loop in JavaScript:

Javascript




const arr1 = [8, 9, 3, 7, 5, 13];
const arr2 = [12, 9, 5, 18, 23];
 
const iterativeMultiply = (arr) => {
    let result = 1;
    for (let i = 0; i < arr.length; i++) {
        result *= arr[i];
    }
    return result;
}
 
console.log(iterativeMultiply(arr1));
console.log(iterativeMultiply(arr2));


Output

98280
223560

Time Complexity: O(n), n is the size of the passing array.

Space Complexity: O(1)

Using the in-built methods:

There are some in-built array methods available in JavaScript which we can use to iterate through the array elements and make changes in their values by passing a callback function inside them. We will use the map() and the reduce() in-built methods in this approach to multiply the elements of an array.

Example: The below code example will explain the use of the in-built JavaScript methods to multiply the elements of an array:

Javascript




let arr1 = [8, 9, 3, 7, 5, 13];
let arr2 = [12, 9, 5, 18, 23];
 
let mapRes = 1;
arr1.map((currItem) => {
    mapRes *= currItem;
});
 
const reduceRes = arr2.reduce((res,
    currItem) => res *= currItem)
 
console.log(mapRes);
console.log(reduceRes);


Output

98280
223560

Time Complexity: O(n), n is the size of the passing array.

Space Complexity: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads