Created
December 25, 2018 17:07
-
-
Save gisderdube/80b60409995a9e8c1b6fbb8f7b2613a1 to your computer and use it in GitHub Desktop.
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 calculate({operands = [1, 2], type = 'addition'} = {}) { | |
return operands.reduce((acc, val) => { | |
switch(type) { | |
case 'addition': | |
return acc + val | |
case 'subtraction': | |
return acc - val | |
case 'multiplication': | |
return acc * val | |
case 'division': | |
return acc / val | |
} | |
}, ['addition', 'subtraction'].includes(type) ? 0 : 1) | |
} | |
console.log(calculate()) // 3 | |
console.log(calculate({type: 'division'})) // 0.5 | |
console.log(calculate({operands: [2, 3, 4], type: 'multiplication'})) // 24 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment