# 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?

``````function potentiate (result, basis, power) {

if (power === 0) {
console.log(result)
return;
}

power--;
result *= basis;

return potentiate(result, basis, power)

}
``````

@camperextraordinaire 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?

``````function potentiate (basis, power, result = basis) ...
``````
``````function potentiate (basis, power, result) {

result = basis; ...
``````

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)

}
``````

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”

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)

}
``````

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

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.