Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Implement curry()

A Closer Look at Functionshard

Implement `curry(fn)`, which returns a curried version of `fn`. The curried function should keep returning a new function until at least as many arguments as `fn` declares (`fn.length`) have been supplied, then call `fn` with all of them. It must support any split of arguments: `add(1)(2)(3)`, `add(1, 2)(3)`, and `add(1)(2, 3)` should all work.

Examples

Input: const add = curry((a, b, c) => a + b + c); add(1)(2)(3)
Output: 6
Input: add(1, 2)(3)
Output: 6
Arguments can arrive in any grouping.

Sample tests

Input: add(1)(2)(3)
Output: 6
Input: add(1, 2)(3)
Output: 6

+ 2 hidden tests run on Submit.

Hints

Common pitfalls
  • Comparing against a hard-coded argument count instead of `fn.length` only works for one specific function.
  • Losing earlier arguments — accumulate them with `concat`, do not overwrite.

Learning resources

  • MDN: Closures
  • MDN: Functions guide
Approach & explanation (try first)

Currying relies on a closure that remembers the arguments gathered so far. Each call checks whether enough arguments exist to satisfy the original function's arity; if not, it returns another collector closure. Once the threshold is met, the original function runs with the full argument list.

Loading...
⌘/Ctrl + Enter

Run your code to see results.