I implemented Math.pow using a log(n) solution just like this article on geeksforgeeks
http://www.geeksforgeeks.org/write-a-c-program-to-calculate-powxn/
However, I'm finding that the function does not exit its base case as I had intended. This program seems like it works in C but not JS.
Therefore, I am concluding that there is something about C that I am assuming works as well in JavaScript.
What am I missing in my JavaScript implementation?
Be forewarned: the codesnippet as it is will have a max call stack exceeded error
var myPow = function(x, n) {
var res = 1
var temp;
if (n === 0) {
return 1;
}
temp = myPow(x, n / 2)
if (n % 2 === 0) {
return temp * temp
} else {
return x * temp * temp
}
};
console.log(myPow(2,3));