Currently, I am attempting to tackle a specific problem on LeetCode. This particular challenge involves calculating the power of x.
However, upon executing my solution, an error message is displayed:
RangeError: Maximum call stack size exceeded
Here's the snippet of 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 specific test case seems to be causing the failure
I have since made adjustments based on the recommendation of Luca Kiebel:
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);
}
}
If anyone could kindly point out where I might be going wrong, I would greatly appreciate it.