我们需要编写一个JavaScript函数,以使用递归方法来计算数字n的阶乘。
为此的代码将是-
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