Recursion in javascript //16.Making our own stack Please help


#1



The outcome should be an array, power(3,3) =>[3,9,27] but it gives a single value 27.


Replace this line with your code. 
// 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);


#2

do not call it. take this line away.


#3

Hi kyleaw,
I removed it, but how to call the function to see if it works?


#4

If you're returning single values instead of an array, then isn't the solution just a bit of re-ordering of the words in the problem description? What's stopping you?

For example, you return 1, that's clearly not an array, that should be very easy to adjust, but you have not

Confused.

So either just spend more time on this because what you're asking for is very obvious, or motivate why it isn't, because I'm not seeing what information it is you're missing

Motivating what's stopping you from figuring something out is generally really useful information to include in a question


#5

In the description: Assign the statement after the = in the recursive case to stack[exponent - 1].

The return stack[exponent - 1].
The code is passed, but not with the expected result. I did reordered words, but it still doesn't work. I am running out of the ideas what else can be changed.


#6

If there's an array in your function that has the expected values in it.. and you're currently returning one of those values.. then just return the array instead

And if you return the number literal 1 when your function should return an array, then return 1 in an array instead?


#7

The instructions are asking for a single value to be returned, reading over those again might make sense out of things? And also investigate why you expected an array cause maybe there's an a-ha there somewhere to be had


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.