Question as below:
create a sum function, and the requirement:
sum(1,2).result === 3
sum(1,2)(3).result == 6
sum(1,2)(3,4).result == 10
sum(1,2)(3,4)(5).result == 15
This is a question about currying in JS. I have implemented the most functions of the question. The tricky point is .result for me.
What does .result means after sum(1,2)? Is it an attribute?
How to add the .result to my code?
function sum(){
var count = 0;
for(let i=0; i<arguments.length; i++){
count += arguments[i];
}
var tmp = function(){
for(let i=0; i<arguments.length; i++){
count += arguments[i];
}
return tmp;
}
tmp.toString = function(){
return count;
}
return tmp;
}
console.log(sum(1,2))
console.log(sum(1,2)(3))