function power(base, exponent) {
debugger;
if (exponent == 0)
return 1;
else
var b = power(base, exponent - 1);
return base * b;
}
console.log(power(2, 3));
// → 8
So I am using the Chrome Dev Tools to try and peek into what is happening here and still cannot quite grasp it. I have assigned var b to power(base, exponent - 1); instead of just having it run in the else statement because I wanted to view the VALUE of that function being run.
Not sure if this is even explainable over a forum post, but I am trying to really grasp the WHY and the WHAT of what is happening in this basic recursion example.
I can see that this function loops through subtracting 1 from the exponent each time until it gives me the answer, but I don’t see where the values are stored, or where the browser is getting the information to do its math.
Please excuse my lack of wording and understanding to correctly phrase this problem I am having.
What I am confused about is where the multiplying the base * exponent actually happens.
I see that the base will eventually be multiplied by the return of 1, once the exponent == 0. Once that happens however, what is telling this function to continue to multiply the base * return value.
Also, I tried the link you sent but it times out, giving me an error.