计算一个数的因数-JavaScript
我们需要编写一个JavaScript函数,该函数接受一个数字并返回精确除以输入数字的数字计数。
例如-
如果数字为12,则其因子为-
1, 2, 3, 4, 6, 12
因此,输出应为6。
示例
以下是代码-
const num = 12;
const countFactors = num => {
let count = 0;
let flag = 2;
while(flag <= num / 2){
if(num % flag++ !== 0){
continue;
};
count++;
};
return count + 2;
};
console.log(countFactors(num));
console.log(countFactors(2));
console.log(countFactors(454));
console.log(countFactors(99));输出结果
以下是控制台中的输出-
6 2 4 6
热门推荐
1 带清的简短祝福语
9 送蛇的祝福语简短
10 祖国生日祝福语简短英文
11 新年祝福语长篇文案简短
12 佛教生日祝福语简短大全
13 新年给妹妹祝福语简短
14 新年祝福语简短给孩子
15 唯美的祝福语简短的
16 雪后早晨简短的祝福语
17 很暧昧的简短祝福语
18 八十大寿简短祝福语