I'm trying to solve the following problem on CodeWars: A Chain adding function:
We want to create a function that will add numbers together when called in succession.
add(1)(2); // returns 3
We also want to be able to continue to add numbers to our chain.
add(1)(2)(3); // 6 add(1)(2)(3)(4); // 10 add(1)(2)(3)(4)(5); // 15
and so on.
A single call should return the number passed in.
add(1); // 1
We should be able to store the returned values and reuse them.
var addTwo = add(2); addTwo; // 2 addTwo + 5; // 7 addTwo(3); // 5 addTwo(3)(5); // 10
We can assume any number being passed in will be valid whole number.
I tried for many hours but to no avail. Like for eg.
add(1)(2)(4) <--- 3 chained functions
I learned about what "currying" is but returning a function inside my main function would only satisfy add(1)(2)
<-- 2 chained functions. Again, I'm trying to chain any number of functions here and I can't seem to figure it out.
If the sample test is add(5)(13)(3)(10)(5)(6)(20)
that's 7 functions chained...I can't possibly type 7 functions in a row return inside one before another except for the first one (main function).
I would like to ask is there any way to check the length of chained functions? Aside from my question, I would appreciate some tips, I'm not asking for a full blown answer.