Power of 2
jessberr (6)

https://repl.it/@jessberr/Power-of-2

Any hints of how to make this function print out an array of values like [1,3,9] instead of [3^0, 3^1, 3^2]. I'm sure it's simple, but I've gone down a rabbit hole trying to figure out what to do. Would I use something like reduce? Or would it be changing line 5 to push something different?

Thanks for everyone's patience?

You are viewing a single comment. View All
Geocube101 (645)

On line 5:
replace 'result.push(num+ '^' +i)' with 'result.push(eval(num+ '**' +i));'

eval() takes valid code and evaluates it. In this case, '**' is the exponent operator. When evaluated, it returns 'num' to the power of 'i'.

New Code:
function exp(num){
var result =[];

for (var i=0; i<=num; i++){
result.push(eval(num+ '**' +i));
}

return result;
}

exp(3)

jessberr (6)

@Geocube101 Thank you! I had seen eval() but wasn't sure how to use it, so thank you so much!