I'm trying to solve this leetcode problem. In this problem, I basically need to return the power of x.
But when I run my solution it returns this error message:
RangeError: Maximum call stack size exceeded
My code:
function myPow(base, exponent){
if(exponent === 0) {
return 1;
} else if(exponent < 0){
return (myPow(base,exponent + 1)/base);
} else {
return base * myPow(base,exponent-1);
}
}
myPow(0.00001, 2147483647) // this test case is failing only I think
Updated code according to Luca Kiebel's suggestion
function myPow(base, exponent){
if(exponent === 0) {
return 1;
} else if(exponent < 0){
return (myPow(base,exponent + 1)/base);
} else {
return base ** myPow(base,exponent-1);
}
}
Anyone please explain me where I'm making a mistake.