implement Math.pow()
2022年5月1日小于 1 分钟
Math.pow()
implement Question
Can you write your own Math.pow() ? The power would only be integers.
pow(1, 2)
// 1
pow(2, 10)
// 1024
pow(4, -1)
// 0.25
All inputs are safe.
Follow-up
You can easily solve this problem by multiplying the base one after another, but it is slow. For power of n
, it is needed to do the multiplication n
times, can you think of a faster solution ?
Code
/**
* @param {number} base
* @param {number} power - integer
* @return {number}
*/
function pow(base, power){
// your code here
}