- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculating the sum of digits of factorial JavaScript
We are required to write a JavaScript function that takes in a number. The function should first calculate the factorial of that number and then it should return the sum of the digits of the calculated factorial.
For example −
For the number 6, the factorial will be 720, so the output should be 9
Example
const factorial = (num) => { if (num == 1) return 1; return num * factorial(num-1); }; const sumOfDigits = (num = 1) => { const str = num.toString(); let sum = 0; for (var x = -1; ++x < str.length;) { sum += +str[x]; }; return sum; }; const sumFactorialDigits = num => sumOfDigits(factorial(num)); console.log(sumFactorialDigits(6));
Output
This will produce the following output −
9
- Related Articles
- Calculating factorial by recursion in JavaScript
- Calculating a number from its factorial in JavaScript
- Find sum of digits in factorial of a number in C++
- Returning number of digits in factorial of a number in JavaScript
- Repeated sum of Number’s digits in JavaScript
- Recursive sum all the digits of a number JavaScript
- Prime digits sum of a number in JavaScript
- Destructively Sum all the digits of a number in JavaScript
- Product sum difference of digits of a number in JavaScript
- Calculating resistance of n devices - JavaScript
- Calculating median of an array JavaScript
- Calculating the weight of a string in JavaScript
- Calculating the LCM of multiple numbers in JavaScript
- Digit sum upto a number of digits of a number in JavaScript
- Difference between product and sum of digits of a number in JavaScript

Advertisements