 # FE Interview Question - Implement Currying

## Instructions

Create a function sum that implements currying. Currying is a transformation of functions that translates a function from being callable as f(x, y, z) into callable as f(x)(y)(z). Currying doesn’t call a function. It just transforms it.

In this question, Implement a sum function that can take n number of parameters and calls and outputs the resulting sum of all the numbers that are passed into it.

Example

``````// sum implementation hereconst sum = () => {
...
...
}
console.log(sum(1)(2)(5)());

// Output: 8``````

Here, the output will be 8 because the sum function takes in 12, and 5 outputting the sum of all the three numbers as 8.

## What is currying?

Currying is a technique in JavaScript where a function with multiple arguments is transformed into a sequence of functions with a single argument. The result of each function is a new function that takes the next argument in the sequence until all arguments have been collected and the original function can be applied. This allows for more flexible function composition and is often used in functional programming.

### In this Example:

``````const sum = (a) => {
const add = (b) => {
if (b) {
return sum(a + b);
} else {
return a;
}
};
}
``````

This is a programming task that requires creating a function called sum that implements currying. Currying is a technique where a function with multiple arguments is transformed into a sequence of functions with a single argument. The sum function should take in n number of parameters and calls and output the resulting sum of all the numbers that are passed into it.

In the given example, the sum function is first called with 1, which returns a new function that expects 2 as its argument. This new function returns another function that expects 5 as its argument. Finally, this last function is called with an empty argument list, which causes the sum of 1, 2, and 5 to be returned and printed to the console using console.log().

A sample implementation of the sum function is provided in the example as well, which uses recursion to add up the numbers.

``````function sum() {
let args = Array.prototype.slice.call(arguments);
args.push(...arguments);
};