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


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
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];

<do not remove the three backticks above>

do not call it. take this line away.

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

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


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

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.

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?

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

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