I want a recursive function that returns the powers of a number and stores each of them in an array called *stack*.
In other words, each time the recursion is executed, a new value will be added to the stack.
For example, if we called power(3, 3), our stack should end up with the elements [3, 9, 27].
However, the outcome of this code is 27 instead of the array. What is my mistake?
// Create an empty array called "stack"
var stack = [];
// Here is our recursive function
function power(base, exponent) {
// Base case
if ( exponent === 0 ) {
return 1;
}
// Recursive case
else {
stack[exponent - 1] = base * power(base, exponent - 1);
return stack[exponent - 1];
}
}
power(3,3);