freeCodeCamp Challenge Guide: Factorial

Factorial


Solutions

Solution 1 (Click to Show/Hide)
function factorial(n) {
    return n <= 1 ? 1 : n * factorial(n - 1);
}

The factorials are calculated through a recursive, conditional loop until 1 is reached.

This solution can also be represented as an arrow function:

const factorial = n =>
    n <= 1 ? 1 : n * factorial(n - 1);
1 Like