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)
}
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…
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)
}