Skip to content

Latest commit

 

History

History
72 lines (56 loc) · 1.4 KB

_2629. Function Composition.md

File metadata and controls

72 lines (56 loc) · 1.4 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 09, 2024

Last updated : July 13, 2024


Related Topics : N/A

Acceptance Rate : 86.66 %


Solutions

JavaScript

/**
 * @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;
    }
};