
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
Calculate 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
Advertisements