Last active
March 10, 2020 01:46
-
-
Save bekliev/058b1e4838a635a6d8ccf45d90004619 to your computer and use it in GitHub Desktop.
sum function which returns another function
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
(() => { | |
console.clear(); | |
console.log('sum is: ' + sum(1)(2)(3)(4)); | |
function sum(a, b = 0) { | |
const res = a + b; | |
console.log({res, a, b}); | |
fn = (b) => sum(res, b); | |
fn.toString = fn.valueOf = () => res; | |
return fn; | |
} | |
})(); |
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
(() => { | |
console.clear(); | |
console.log('sum is: ' + sum(1,2,3)(4,5)); | |
function sum(...nums) { | |
const res = nums.reduce((a, b) => a + b); | |
console.log({...nums, res}); | |
fn = (...nums) => sum(res, ...nums); | |
fn.toString = fn.valueOf = () => res; | |
return fn; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment