Returning number of digits in factorial of a number in JavaScript


Problem

We are required to write a JavaScript function that takes in a number, num, as the first and the only argument.

Our function should compute and return the number of digits in the factorial of the number num.

For example, if the input to the function is −

Input

const num = 7;

Output

const output = 4;

Output Explanation

Because the value of 7! Is 5040 which contains 4 digits.

Example

Following is the code −

 Live Demo

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));

Output

4

Updated on: 22-Apr-2021

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements