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 here
const sum = () => {
...
...
}
console.log(sum(1)(2)(5)());
// Output: 8
Here, the output will be 8
because the sum
function takes in 1
, 2
, and 5
outputting the sum of all the three numbers as 8
.
Click to reveal
Can you try using recursion in this problem?
Click to reveal
Try returning a function from within a function. How do you calculate the sum of two numbers? Can you pass the sum as the parameter to the same function? Did you study? -_-