pwstrick / daily

一份搜集的前端面试题目清单、面试相关以及各类学习的资料(不局限于前端)
2.39k stars 242 forks source link

2的幂 #1030

Open pwstrick opened 4 years ago

pwstrick commented 4 years ago

231. 2的幂

/**
 * @param {number} n
 * @return {boolean}
 */
var isPowerOfTwo = function(n) {
    let index = 0;
    while(true) {
        let digit = Math.pow(2, index);
        if(digit == n) {
            return true;
        }
        if(digit > n) {
            break;
        }
        index++;
    }
    return false;
};