FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Curry

15mins

Currying is the process of transforming a function that takes multiple arguments into a sequence of functions that each take a single argument.

Implement a function curry(fn) that accepts a function as its only argument and returns a new function.
The returned function should keep accepting arguments one by one until enough arguments have been provided to call the original function. Then it should invoke the original function with those arguments.

Arguments:

  • fn (Function): The function to be curried.

Returns:

  • Function: A new function that supports currying.

Examples:

function add(a, b) { return a + b; } const curriedAdd = curry(add); curriedAdd(3)(4); // 7 const addThree = curriedAdd(3); addThree(4); // 7 function multiplyThreeNumbers(a, b, c) { return a * b * c; } const curriedMultiply = curry(multiplyThreeNumbers); curriedMultiply(4)(5)(6); // 120 const withFour = curriedMultiply(4); const withFourAndFive = withFour(5); withFourAndFive(6); // 120

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests