How to use the array methods In Javascript
In this approach, we will use JavaScript array methods like array constructor, fill and reduce methods to calculate the power of the given number
Syntax:
// Creating array
arr = new Array(total elements).fill(value)
arr.reduce((accumulator , value)=> accumulator*value, initial value of accumulator)
Example: In this example, we will print the value of 2 to power 5
// Base number input
let n = 2;
// Power input
let power = 5;
// Creating new array having value n
let numArray = new Array(power).fill(n);
// Calculate the result output
let result = numArray.reduce((res, n) => (res *= n), 1);
// Display output
console.log(result);
Output
32
JavaScript Program to Compute Iterative Power of a Number
This article will demonstrate how to compute the iterative power of a number using JavaScript. It will include iterative methods to calculate and display the power of a given number.