Skip to content

Instantly share code, notes, and snippets.

@abhinavnigam2207
Created April 5, 2019 07:40
Show Gist options
  • Save abhinavnigam2207/e9812754b61e1e5f6ebf02804412ab41 to your computer and use it in GitHub Desktop.
Save abhinavnigam2207/e9812754b61e1e5f6ebf02804412ab41 to your computer and use it in GitHub Desktop.
Sum multiple notations
function sum(...args) {
var resp = args.length==1 ? args[0] : args.reduce((key, acc) =>acc+=key,0);
function innerSum(...args1) {
let b = args1.length==1 ? args1[0] : args1.reduce((key, acc) =>acc+=key,0);
resp += b;
return innerSum;
}
innerSum.toString = function(){
return resp;
}
return innerSum;
}
sum(1)(2)(3)(4)(5)(6);
sum(1,2,3,4,5,6);
sum(1,2)(3)(4,5,6);
@abhinavnigam2207
Copy link
Author

Write a generic function Sum.
Which returns sum of all numbers on calling it in different fashion. How do you implement this?

Sum(1)(2)(3)(4)(5)(6) - 21
Sum(1,2)(3)(4,5,6) - 21
Sum(1,2,3,4,5,6) - 21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment