Created
April 5, 2019 07:40
-
-
Save abhinavnigam2207/e9812754b61e1e5f6ebf02804412ab41 to your computer and use it in GitHub Desktop.
Sum multiple notations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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