Ad

i like to keep the accumulated value in an optional parameter.

Code
Diff
  • const factorial = (n, product=1) => n < 2 ? product : factorial(n-1, n*product);
    • function factorial (n) {
    • if (n === 1) {
    • return 1;
    • } else {
    • return n * factorial(n - 1);
    • }
    • }
    • const factorial = (n, product=1) => n < 2 ? product : factorial(n-1, n*product);