我们需要编写一个JavaScript函数,以使用递归方法来计算数字n的阶乘。
在这里,我们找到阶乘递归并创建一个自定义函数recursiceFactorial()-
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));
让我们为该函数编写代码-
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));
输出结果
以下是控制台中的输出-
362880 720 3628800 120 6227020800