We are required to write a JavaScript function that computes the Factorial of a number n by making use of recursive approach.
Here, we are finding the factorial recursion and creating a custom function recursiceFactorial() −
const num = 9; const recursiceFactorial = (num, res = 1) => { if(num){ return recursiceFactorial(num-1, res * num); }; return res; };
Now, we will call the function and pass the value to find recursion −
console.log(recursiceFactorial(num)); console.log(recursiceFactorial(6)); console.log(recursiceFactorial(10));
Example
Let’s write the code for this function −
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
Following is the output in the console −
362880 720 3628800 120 6227020800