
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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
Advertisements