Scriptdrill

Level 5: Async and functions Exercise 43 of 45 Pro

Pipe functions together

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

Return a function that passes its input through `fns` from left to right: `pipe(f, g, h)(x)` is `h(g(f(x)))`. The first function may take several arguments; the others get one. With no functions, the result returns its first argument unchanged.

What this exercise teaches

Only the first function sees the original arguments, so it is called separately with ...args; reduce then threads one value through the remaining functions. Handling the empty list first makes pipe() an identity function.

A hint if you get stuck

Return `(...args) => ...`. Call the first function with all of `args`, then `reduce` over the rest, feeding each result into the next.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Async and functions