Skip to content

Instantly share code, notes, and snippets.

@gisderdube
Created December 25, 2018 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gisderdube/80b60409995a9e8c1b6fbb8f7b2613a1 to your computer and use it in GitHub Desktop.
Save gisderdube/80b60409995a9e8c1b6fbb8f7b2613a1 to your computer and use it in GitHub Desktop.
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