PineNuts0
PineNuts0

Reputation: 5234

JavaScript: Trying to Understand The Else Statement of a Recursion Function Calculating Exponent Value

I want to calculate exponents using recursion. I have the code below which executes successfully.

function pow(base, exponent) {

  if (exponent <= 1){
    return base
  }

  else {
    return base * pow(base, exponent-1)
  }
}

// console.log(pow(3, 5)); // -> answer is 243

I am trying to understand the else case here. In the else statement when the input argument for exponent is 2 or higher:

what does the pow(base, exponent-1) portion of return base * pow(base, exponent-1) return? Does it equal the base value?

Upvotes: 2

Views: 76

Answers (4)

Scott Sauyet
Scott Sauyet

Reputation: 50807

So if you want to calculate `pow(2, 3) -- that is 2 raised to the 3rd power, or 8 -- this function does

                                                       (if)
                                         since 1 <= 1 ------+
                               (else)                       |
                   since 2 > 1 ------+                      |
                                     |                      |
            (else)                   |                      |
since 3 > 1 ------,                  |                      |
                  V                  V                      V
       pow(2, 3) ---> 2 * pow(2, 2) ---> 2 * 2 * pow(2, 1) ---> 2 * 2 * 2 -> 8

This is the essence of recursion: calling a function from inside the same function (or at least somewhere in the call-stack; see mutual recursion examples), using data that is in some way simpler; so that eventually you hit a base case you can calculate without such calls.

Upvotes: 2

JSONaLeo
JSONaLeo

Reputation: 416

On each recursion, the function is called with 1 less than the exponent initially passed.

pow(base, exponent-1)

essentially counting down from it's initial value to 1, at which point it meets the condition where it stops recursing, and only returns the base.

if (exponent <= 1){
    return base
}

so if you pass pow(3, 4),

recursion 1 (pow(3,4)): 3 * // 3

recursion 2 (pow(3,3)): 3 * // 9

recursion 3 (pow(3,2)): 3 * // 27

recursion 4 (pow(3,1)): 3 = // 81

Upvotes: 0

Kamran Koupayi
Kamran Koupayi

Reputation: 36

It's a recursion function. Consider the recursion function F(n) in mathematic. F(n, m) = n * F(n , m-1), F(n, 1) = n. So, the code just implement this function.

F(n) in the code is pow(base, exponent) which n is base and m is exponent.

Upvotes: 0

Mark
Mark

Reputation: 92450

Consider that 2 ** 3 == 2 * (2 ** 2) and 2 ** 2 == 2 * (2 ** 1)

Substituting gives:

2 ** 3 == 2 * 2 * (2 ** 1)

This is all your recursive function is doing. When you call:

pow(2, 3)

That turns into:

base * pow(2, 2)

pow(2, 2) turns into:

 base * pow(2, 1)

substitute give:

base * base * pow(2, 1)

pow(2, 1) is your base case which equals base so in the end you get

pow(2, 3) === base * base * base

One of the best tools for understanding recursion is the debugged. Just step through and see how the values are changing and what's on the stack.

Upvotes: 1

Related Questions