Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Compose Functions

A Closer Look at Functionsmedium

Course · Section 10: A Closer Look at Functions · Lecture 139: Functions Returning Functions

Implement `compose(...fns)` returning a function that applies the given functions right to left: `compose(f, g)(x)` equals `f(g(x))`.

Examples

Input: const inc = x => x+1; const dbl = x => x*2; compose(dbl, inc)(3)
Output: 8
inc(3) is 4, then dbl(4) is 8 (right to left).

Sample tests

Input: compose(dbl, inc)(3)
Output: 8
Input: three functions
Output: 3

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Applying left-to-right instead of right-to-left (that would be pipe).
Visualize this concept: Currying →
Approach & explanation (try first)

compose folds the functions from the right so the rightmost runs first, matching mathematical composition.

Loading...
⌘/Ctrl + Enter

Run your code to see results.