-
-
Save gil00pita/205c711a84c6031b4428323e028f642d to your computer and use it in GitHub Desktop.
https://levelup.gitconnected.com/9-tricks-for-kickass-javascript-developers-in-2019-eb01dd3def2a ### Destructuring & default values Let’s return to our previous example where we do the following: ```
const result = axios.get(`https://ironhack-pok
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