
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding Nth Power of Array Element 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 −
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 ]
Advertisements