求x的n次方 js

方法一

使用js内置的方法

Math.pow(x, y)

方法二

使用循环

function myPow(x, n) {
            let pow = 1;
            for (let i = 1; i <= n; i++) {
                pow = pow * x
            }
            return pow;
        }

方法三

用分治法
参考这边文章 https://leetcode-cn.com/problems/powx-n/solution/powx-n-by-leetcode-solution/

 function myPow(x, n) {
            if (n == 0) {
                return 1;
            }
            let y = myPow(x, Math.floor(n / 2));
            return n % 2 == 0 ? y * y : y * y * x;
        }
原文地址:https://www.cnblogs.com/heihei-haha/p/14255083.html