I faced this question in one interview. I did not get how to solve this. Question: Write a sum function which will add 2 numbers, but numbers can be passed to a function in following ways:
- sum(3)(4) // answer should be 7
- sum(3)()(4)//answer should be 7
- sum(3)()()()()(4) //answer should b 7
I can solve first function using closure, in fact for the second function also I can check the arguments and if arguments length is zero I can again make a call to sum to except next parameter. But how to make it generic ? Means even your first parameter and last parameter has 'N' number of calls & those can be empty or parameterized, it should return sum.