题解 | #幂#
幂
http://www.nowcoder.com/practice/8865ac17c9bd4adeb01e16b141fd197f
关于幂为非正整数的讨论
function _pow(number,power) {
// 补全代码
let result = 1;
// 判断power是否小于零
let flag = false;
if (power < 0) {
power = (-1) * power;
flag = true;
}
// power等于0,跳出循环,result == 1
for (var i = 0; i < power; i++) {
result *= number;
}
// 如果power小于零,返回result的倒数
if (flag) {
result = 1 / result;
}
return result;
}