2741. Function Composition
My accepted JavaScript solution to LeetCode problem 2741, Function Composition, running in 50ms.
- Difficulty: Easy
- JavaScript
- Runtime 50ms
- Memory 54.9MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
JavaScript
Accepted on LeetCode — runtime 50ms, memory 54.9MB, accepted 2026-01-01.
/**
* @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
*/