2629. Function Composition
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : July 09, 2024
Last updated : July 13, 2024
Related Topics : N/A
Acceptance Rate : 86.66 %
/**
* @param {Function[]} functions
* @return {Function}
*/
var compose = function(functions) {
return function(x) {
output = 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
*/
var compose = function(functions) {
return function(x) {
output = x
functions.reverse().forEach(el => output = el(output));
return output;
}
};
var compose = function(functions) {
return function(x) {
functions.reverse().forEach(el => x = el(x));
return x;
}
};