Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Alex Flores
7,864 PointsTrying to understand JavaScript recursion.
I have this function that creates gives the exponential function:
<script>
function power(base, exponent) {
if (exponent == 0) {
return 1;
}
else {
return base * power(base, exponent - 1);
}
}
console.log(power(2, 3));
</script>
I think I what I don't get is something very basic, but I don't understand how it returns the correct answer (8). I would think that it would just return 1, since that's where the program finally stops. The way I understand it, is the code runes three times, until exponent reaches 0. Each time it runs it multiples itself base. So we have base x base x base = 8. But I don't understand why the compiler returns 8?
Any help on this, would be appreciated.
2 Answers
Seth Kroger
56,416 PointsBecause we are recursively calling the power function for progressively smaller powers, the call stack has multiple calls to power in it. When we hit the "bottom" with an exponent of zero, that call returns back to the previous call, which then which multiplies that answer to the base and returns that to the call above it, until the entire stack unwinds and returns the final answer.
power(2, 3);
//--v
return base * power(base, exponent - 1);
return 2 * power(2, 2);
//--------------v
return base * power(base, exponent - 1);
return 2 * power(2, 1);
//--------------v
return base * power(base, exponent - 1);
return 2 * power(2, 0);
//--------------v
return 1;
//-----v
return 2 * 1;
//-----v
return 2 * 2;
//-----v
return 2 * 4;
//-----v
return 8;
Kevin Korte
28,149 PointsAnd I just realized I was approaching this question wrong, I missed the recursion part which was causing more confusion.
Alex Flores
7,864 PointsHey Kevin,
I'm not sure if I'm following you here. I guess another way of asking is, how many times does the compiler iterate through the function? Or another way of saying it, is it correct to say that base * power is never actually calculating, rather it's really just base*= through 3 iterations?
Kevin Korte
28,149 PointsKevin Korte
28,149 PointsGreat explanation. I completely overlooked the recursion part in my initial answer. This should be marked best answer by Alex.
Alex Flores
7,864 PointsAlex Flores
7,864 PointsHey Seth,
Thanks for the great explanation. I definitely understand the top half of your explanation, but I don't really understand the bottom half. What is the 2 multiplying by (e.g., 2 * 1; 2 *2; 2 * 4)? Does power = [1,2,4]? I used to think base was just multiplying itself upon itself (e.g., 2 * 2 *2), because 2 * power(2, n) doesn't make any sense to me, because what value does power take, as it has to arguments?
I don't know if this made any sense.