Recursive Exponentiation Function - one variable too much?

Dear coding community!

I am trying to get rid of the “result” . I imagine being a user and using my function. When looking for e.g. 2^4, it would seem counter-intuitive to call the function with (2, 2, 4) … any suggestions?

Thanks in advance!

function potentiate (result, basis, power) {
  
  if (power === 0) {
    console.log(result)
    return;
  }

  power--;
  result *= basis;
  
  return potentiate(result, basis, power)

}

First of all, you could move the result parameter to be the 3rd parameter in the function declaration, so when the function is called by the user, only the first two arguments are used.

When the function is first called, what value should result be if nothing is passed for the 3rd argument? Once you figure that out, then you just need a check for this situation and assign the appropriate value to result within the function, so the remaining calculations work.

On a separate note, are you sure you want to return undefined if 0 is passed to the power parameter?

Also, what about negative values of power?

1 Like

@RandellDawson thanks for the thorough analysis of my interim solution.

I’ll tackle unintended uses of the function in the next step.

I changed the order of the parameters as you suggested

As for the “result” variable, anytime I link it to the “basis” variable (by defaulting it to it, or assigning it at the top of the function), it takes on the updated value of “basis” and hence its value is skewed…

Any further advice on this?

Show me what you have tried.

1 Like
function potentiate (basis, power, result = basis) ...
function potentiate (basis, power, result) {
  
  result = basis; ...

This could be fine depending on the rest of your function.

1 Like

Thanks so much, I missed that I had both 1) and 2) … the final version works now!

function potentiate (basis, power, result = basis) {
  
  if (power <= 1) {
    console.log(basis)
    return basis;
  }

  basis *= result;
  power--;
  
  return potentiate(basis, power, result)

}

You still need to handle the case where power is 0 like:

potentiate(3, 0) // 1

Actually, your function is not working properly.

potentiate(2, 4) // should result in 32 but your function returns 16

Actually , your function is not working properly.

I thought I could skip giving the recursive call the result parameter, that was the reason it was “off by 1 round”

No matter what, you will still need result *= basis before the recursive call line.

I made another if statement dealing with power === 0, it seems to work but I don’t know why the function now is not ALWAYS returning 1 since the power ends up being 0 in both cases ( I confirmed it with console.log statements)

function positiveExponantiation (basis, power, result = basis) {
  
  if (power === 0) {
    return 1;
  }

  if (power <= 1) {
    console.log(basis)
    return basis;
  }

  basis *= result;
  power--;
  
  return positiveExponantiation(basis, power, result)

}

positiveExponantiation(2, 4) is still returning 16 instead of 32.

This is not going to work. I said you will need to have result *= basis before the recursive call.

strange… maybe I mixed up the variables, I am not an English speaker, but 2^4 is 16 … what am I missing?