Problem I’m trying to solve: A Chain adding function | Codewars
Basically, I was tasked to chain an unknown number of functions to add numbers together. 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.
im not well familiar with currying, but by the looks of it, recursion might be the way to do that
The solution to this Kata is based on a rarely used “trick” which in my opinion doesn’t make for a very good challenge, but to each their own. The gist of it is that the function
add doesn’t actually return a numerical value, it returns an object/function which is coerced into a numerical value by overriding the
valueOf method of Object and that coercion happens in the test runner when the return value of
add is compared to a number using loose equality (
I would suggest you just look at the solutions (that’s what I did) and be pleasantly surprised that you learned some “cool” trick that you would most likely never use in the real world, and then forget about this one completely.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.