Last active
April 19, 2018 09:33
-
-
Save meandmax/a54194843f575618d48bb6f00b408672 to your computer and use it in GitHub Desktop.
Add Rekursion
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
const add = (y) => { | |
return (x) => { | |
if (!x) { | |
return y; | |
} | |
return add(y + x); | |
} | |
} | |
// add(1)(2)(3)() | |
function add(y) { | |
let sum = 0; | |
function add (x) { | |
for (let i=0; i<arguments.length; i++) { | |
sum += Number(arguments[i]); | |
} | |
return add; | |
} | |
add.valueOf = function valueOf(){ | |
return sum; | |
}; | |
return add.apply(null,arguments); | |
} | |
// add(1)()(3) | |
// even easier ... | |
const add = x => { | |
const inner = y => add(x + y); | |
inner.valueOf = () => x; | |
return inner; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment