LeetCode solutions

2741. Function Composition

My accepted JavaScript solution to LeetCode problem 2741, Function Composition, running in 50ms.

  • Difficulty: Easy
  • JavaScript
  • Runtime 50ms
  • Memory 54.9MB

Read the problem on LeetCode View on GitHub

JavaScript

Accepted on LeetCode — runtime 50ms, memory 54.9MB, accepted 2026-01-01.

javascript
/**
 * @param {Function[]} functions
 * @return {Function}
 */
var compose = function(functions) {
    return function(x) {
        for (let i = functions.length - 1; i >= 0; i--) {
            x = functions[i](x);
        }
        return x;
    }
};

/**
 * const fn = compose([x => x + 1, x => 2 * x])
 * fn(4) // 9
 */

Source