Solution to Y-Combinator

/**
 * 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

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').


I blurred the code as well just in case so it doesn’t spoil anything.