vendredi 14 juin 2019

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?

Aucun commentaire:

Enregistrer un commentaire