Created
March 16, 2023 10:46
-
-
Save ABHISHEK-KEDAR-21/52b40a3f918c3044befe88a7dd2e5897 to your computer and use it in GitHub Desktop.
cuurrying in JS
This file contains hidden or 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) { | |
let total = 0; | |
let adjust = -1 | |
if (args.length && args[0] === 0) { | |
adjust = 0 | |
} | |
function inner(...innerArgs) { | |
if (innerArgs.length === 0) { | |
return total | |
} | |
args = [...args, ...innerArgs] | |
total = args.reduce((acc, i) => acc + parseInt(i), adjust) | |
return inner | |
} | |
return inner(args) | |
} | |
console.log(sum(0,0,0)()) // 0 | |
console.log(sum(0,1)(1)()) // 2 | |
console.log(sum(0,1)(1)(2,3)(2)()) // 9 | |
console.log(sum(0,0)(1)(2,3)(0)()) // 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment