/**
* Y combinator function for creating recursive functions.
*
* @param {Function} f - The stateless function to make recursive.
* @returns {Function} - A recursive version of the input function.
*/
const Y = (f) => ((x) => f(x(x)))(x => f(y => x(x)(y)));
/**
* Factorial function.
*
* @param {number} n - The number for which to calculate the factorial.
* @returns {number} - The factorial of the input number.
*/
const factorial = Y((self) => (n) => (n === 0 ? 1 : n * self(n - 1)));
// Example usage
console.log(factorial(5)); // Output: 120
console.log(factorial(0)); // Output: 1
console.log(factorial(10)); // Output: 3628800
https://www.freecodecamp.org/learn/coding-interview-prep/rosetta-code/y-combinator