How to use the Math Object In Javascript
JavaScript’s Math object provides several methods for mathematical operations. We can use the Math object to multiply all the elements of an array using the Math.pow() method, which calculates the power of a number.
Example:
function multiplyArray(arr) {
return arr.reduce((acc, curr) => acc * curr, 1);
}
const array = [1, 2, 3, 4, 5];
console.log(multiplyArray(array));
Output
120
Multiply the elements of an array in JavaScript
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:
Table of Content
- Recursive Method:
- Iterative Method:
- Using the in-built methods:
- Using forEach method
- Using the Math Object: