How to implement a function sum which will take parameters like the below and should give the output of the total sum without calling it at the end with an empty parameter call.
sum(1,2..n)(5,6…n)…(n)
example:
console.log("output should be: ", sum(1)(2,3)(4,5,6)) // output should be: 21
Note:
I know we can use valueOf but that will only work if we are parsing the result in primitive value.
I don't want solution for console.log(sum(1)(2,3)(4,5,6)()) // 21 as its solution I know which is:
function add(...args) {
let a = args.reduce((a, b) => a + b, 0)
function sum(...args){
let b = args.reduce((a, b) => a + b, 0)
if(b){
return add(a+b)
}
return a
}
return sum
}