Description

In JavaScript, functions can take multiple arguments, but sometimes we want more control over how those arguments are passed.


That’s where currying comes in.

Currying is a functional programming technique that transforms a function with multiple parameters into a sequence of functions, each taking one argument at a time.

It’s often used to make functions more reusable, readable, and easier to compose — especially in scenarios like event handling, configuration functions, and utility libraries.


For example, instead of writing:


add(2, 3)


you can curry it so it can be called like this:


add(2)(3)


This allows partial application — meaning you can pass some arguments now and others later, which is very useful in real-world applications.

Your Task

Implement a function curry(fn) that transforms any normal JavaScript function into its curried version.

The returned function should keep accepting arguments until all expected parameters are received. Once all arguments are provided, it should execute the original function and return the result.

Examples

Input

function add(a, b, c) {
return a + b + c;
}

const curriedAdd = curry(add);

console.log(curriedAdd(1)(2)(3));

Expected Output

6

Explanation

  • The add function normally expects 3 parameters at once.
  • After applying curry(add), we can call it one argument at a time: curriedAdd(1)(2)(3).
  • Once all required arguments are collected, the original function executes and returns the sum.

This mimics how currying works in functional libraries like Lodash or Ramda — a fundamental pattern in functional programming and modern JavaScript development.

Example 2 (Partial Application)

const multiply = (a, b) => a * b;
const curriedMultiply = curry(multiply);

const double = curriedMultiply(2);
console.log(double(5)); // 10

Here, double becomes a reusable function that multiplies any number by 2.