在 JavaScript 中返回数字阶乘的位数

javascriptweb developmentfront end technology

问题

我们需要编写一个 JavaScript 函数,该函数将数字 num 作为第一个也是唯一的参数。

我们的函数应该计算并返回数字 num 阶乘的位数。

例如,如果函数的输入是 −

输入

const num = 7;

输出

const output = 4;

输出说明

因为 7! 的值为 5040,包含 4 位数字。

示例

以下是代码 −

const num = 7;
const countDigits = (num = 1) => {
   let res = 0;
   while(num >= 2){
      res += Math.log10(num);
      num--;
   };
   return ~~res + 1;
}
console.log(countDigits(num));

输出

4

相关文章