在 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 诗词送行祝福语大全简短
18 拥有爱的祝福语简短