0
 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);
asked Dec 1, 2016 at 22:18
3
  • Please edit your code to use proper indentation. It's very difficult to read. Commented Dec 1, 2016 at 22:20
  • I appreciate everyone's time and solutions, but I voted for what I can understand. Other codes I need time to study since I am a beginner. Thank you! Also, it has to be a recursion since I am studying recursion but still cannot understand how and when to use it, although I read eloquent javascript several times. Commented Dec 2, 2016 at 3:44
  • I meant eloquent javascript recursion part Commented Dec 2, 2016 at 3:50

2 Answers 2

1

There is no issue with your code. The only issue is the return value. You can see below (BTW, I've minified your code little bit)

var task = (() => {
 var stack = [];
 function power(base, exponent) {
 return (exponent && (stack[--exponent] = base * power(base, exponent))) || 1;
 }
 power(3, 3);
 return stack;
});
console.log(task());

I'm not fan of recursive calls personally and I don't prefer use that pattern unless there is a "have to" case.

So, In your case (Let's forget it is from codeacademy and let's consider that it is kind of real-life case) using a recursive function is not a mandatory.

There are many ways to achieve the same results above.

For example, classic for loop:

function power(base, exp){
 var result = [];
 for(var i=1; i<=exp; i++){
 result.push(Math.pow(base, i));
 }
 return result;
}
console.log(power(3, 3));

Or, ES6 generators (maybe ?)

function *power(base, exp){
 let prev = 1;
 for(var i=0; i<exp; i++){
 yield prev *= base;
 }
}
console.log([...power(3, 3)]);

answered Dec 1, 2016 at 22:40
Sign up to request clarification or add additional context in comments.

Comments

0

Indeed, you don't return the array, but the last calculated product. Actually, the result you are looking for is stored in stack after the call finishes.

But it is bad practice to have a global variable that gets mutated by a function. Instead create a so-called closure for it, and call it powers (plural, to indicate you get an array from it):

function powers(base, exponent) {
 // 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(base, exponent);
 return stack;
}
console.log(powers(3,3)); 

Made a bit more compact:

function powers(base, exponent) {
 var stack = [];
 (function power(exponent) {
 return +!exponent || (stack[--exponent] = base * power(exponent));
 })(exponent);
 return stack;
}
console.log(powers(3,3));

answered Dec 1, 2016 at 22:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.