Sobes.tech
Junior — Middle+

Creating a composition function

livecode

Task condition

Write a function compose that takes an arbitrary number of functions and returns their composition. The function should work correctly with both a single function argument and multiple, passing all received parameters to the first function in the chain.

const square = (x) => x * x;
const times2 = (x) => x * 2;
const sum = (a, b) => a + b;

console.log(compose(square, times2)(2) === square(times2(2)));
console.log(compose(square, times2, sum)(3, 4) === square(times2(sum(3, 4))));