
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Calculating a number from its factorial in JavaScript
We are required to write a JavaScript function that takes in a number as the only argument.
The function should check whether there exists any number whose factorial is the number taken as input.
If there exists any such number, we should return that number otherwise we should return -1.
For example −
If the input is −
const num = 720;
Then the output should be −
const output = 6;
Example
Following is the code −
const num = 720; const checkForFactorial = num => { let prod = 1, count = 1; while(prod <= num){ if(prod === num){ return count; }; count++; prod *= count; }; return -1; }; console.log(checkForFactorial(num)); console.log(checkForFactorial(6565));
Output
Following is the console output −
6 -1
- Related Articles
- Calculating factorial by recursion in JavaScript
- Calculating the sum of digits of factorial JavaScript
- Returning number of digits in factorial of a number in JavaScript
- Function to compute factorial of a number in JavaScript
- Calculating the area of a triangle using its three sides in JavaScript
- Calculating power of a number in MySQL?
- Factorial recursion in JavaScript
- Factorial of a large number
- Calculating h index of a citation in JavaScript
- Calculating the weight of a string in JavaScript
- Calculating average of a sliding window in JavaScript
- Dividing a floating number, rounding it to 2 decimals, and calculating the remainder in JavaScript
- Calculating excluded average - JavaScript
- Calculating Josephus Permutations efficiently in JavaScript
- First digit in factorial of a number in C++

Advertisements