在 JavaScript 中检查数字是否为 4 的有效幂
问题
我们需要编写一个JavaScript函数,它接受一个整数num作为唯一的参数。我们的函数应该检查这个数字是否是有效的4次幂。如果是4的幂,我们应该返回true,否则返回false。
例如,如果函数的输入是-
const num1 = 2356; const num2 = 16;
那么输出应该是-
const output1 = false; const output2 = true;
示例
此代码将是-
const num1 = 2356;
const num2 = 16;
const isPowerOfFour = (num = 1) => {
let bool = false;
for(let i = 0; i < 16; i++){
if( Math.pow(4,i) === num){
bool=true;
return bool;
};
};
return bool;
};
console.log(isPowerOfFour(num1));
console.log(isPowerOfFour(num2));输出结果控制台中的输出将是-
false true
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短