Skip to content

Commit 1ee68b7

Browse files
committed
function composition solution
1 parent f0353a8 commit 1ee68b7

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

easy/function-composition.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
* @param {Function[]} functions
33
* @return {Function}
44
*/
5-
var compose = function (functions) {
6-
return function (x) {};
7-
};
85

9-
/**
10-
* const fn = compose([x => x + 1, x => 2 * x])
11-
* fn(4) // 9
12-
*/
6+
const compose = (functions) => {
7+
return function (x) {
8+
for (let i = functions.length - 1; i >= 0; i--) {
9+
x = functions[i](x);
10+
}
11+
return x;
12+
};
13+
};

0 commit comments

Comments
 (0)