Created
May 20, 2022 18:10
Revisions
-
jacky810124 created this gist
May 20, 2022 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ const sum = (...args) => { let result = 0; if (!args.length) { return result; } return (...a) => { result = args.reduce((accumulator, item) => accumulator + item, result); if (!a.length) { return result; } return sum(...args, ...a); }; }; console.log(`sum(): ${sum()} // 0`); console.log(`sum(1)(): ${sum(1)()} // 1`); console.log(`sum(1)(1)(): ${sum(1)(1)()} // 2`); console.log(`sum(1, 1)(): ${sum(1, 1)()} // 2`); console.log(`sum(1)(1, 1)(1)(): ${sum(1)(1, 1)(1)()} // 4`); console.log(`sum(1): ${sum(1)} // function`); console.log(`sum(1, 1, ..., 1): ${sum(1, 1, 1, 1)} // function`);