I would like solve the function calling with pattern add(n)(m)(o)......(nth) and get the result of addition of n + m + o .... + nth
Constraints: Please do not use global variable(total)
Can I use approach 1 via Approach 2 ?
Approach 1: I tried like this: (Not working)
function CL(){
var t = 0
var add = n => {
this.total += n;
return add;
}
return {
total: t,
add: add
}
}
var res = CL();
res.add(2)(3)(4)(5) ....... (nth) ;
console.log(res.total); // 14 expected
Approach 2: I solve this using global variable(total) like:
var total = 0;
function add(n){
total += n;
return add;
}
add(2)(3)(4)(5);
console.log(total); // now correct 14