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