Syntax error Finding the nth power of array element present at nth index using JavaScript

Finding the nth power of array element present at nth index using JavaScript



Problem

We are required to write a JavaScript function that takes in an array of numbers. Our function should map the input array to another array in which each element is raised to its 0-based index.

And finally, our function should return this new array.

Example

Following is the code −

 Live Demo

const arr = [5, 2, 3, 7, 6, 2];
const findNthPower = (arr = []) => {
   const res = [];
   for(let i = 0; i < arr.length; i++){
      const el = arr[i];
      const curr = Math.pow(el, i);
      res[i] = curr;
   };
   return res;
};
console.log(findNthPower(arr));

Output

[ 1, 2, 9, 343, 1296, 32 ]
Updated on: 2021-04-21T06:46:31+05:30

327 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements