function factorial(n) {
    if (n === 0) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

This implementation of the factorial algorithm uses a recursive approach. It checks whether the input number is equal to zero, and if so, it returns 1. If the input number is not zero, the function calls itself with the input number decremented by 1 and multiplies the result by the input number. This process continues until the input number is 0, at which point the function returns 1 and the previous function calls return their results.

Here's an example of how the function can be used:

console.log(factorial(5));  // 120
console.log(factorial(3));  // 6