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
Calculating factorial by recursion in JavaScript
We are required to write a JavaScript function that computes the Factorial of a number n by making use of recursive approach.
Example
The code for this will be −
const num = 9;
const recursiceFactorial = (num, res = 1) => {
if(num){
return recursiceFactorial(num-1, res * num);
};
return res;
};
console.log(recursiceFactorial(num));
console.log(recursiceFactorial(6));
console.log(recursiceFactorial(10));
console.log(recursiceFactorial(5));
console.log(recursiceFactorial(13));
Output
The output in the console −
362880 720 3628800 120 6227020800
Advertisements