Q:

Incremental powers of elements of an array

I'm trying to take the power of each element in an incremental way, i.e. for an input x = [6,9,5] I want my output to be y = [6², 9³, 5⁴], the power starts from a given variable p.
My code:
var sqr = [];
var n = [6,9,5];
var p = 2;
 for (let i = 0; i < n.length; i++) {
      for (let j = p; j < n.length + 1; j++) {
        sqr.push(Math.pow(n[i],j));
        console.log(sqr);
        i++;
     }
return sqr;

It works but never execute the last elements depending on the size of the array and/or p (I know it's because it goes to an index which doesn't exists). But I'm wondering if there's a better way that works. Thanks

A:

You only need to use one loop.  You can increment the power p inside the loop.

var sqr = [];
var n = [6,9,5];
var p = 2;
for (let i = 0; i < n.length; i++) {    
    sqr.push(Math.pow(n[i],p));    
    p++; 
}
console.log(sqr);

