Taken from DailyCodingProblem
Implement integer exponentiation. That is, implement the
pow(x, y)
function, wherex
andy
are integers and returnsx^y
.Do this faster than the native method of repeated multiplication.
Solution
This is the answer I came up with:
Both functions handle exponentiation: the
exponent
function does it using the exponential operator, and thecustom
function implements it arithmetically. Thecustom
function is probably the more correct answer, but both functions are faster when tested against the nativeMath.pow()
method.