Created
April 10, 2019 04:22
-
-
Save RickJP/c9c88f943997327eea875eb060af10a0 to your computer and use it in GitHub Desktop.
JS - minMax (Return the min and max value in one return statement using reduce)
This file contains hidden or 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 minMax(items) { | |
return items.reduce((acc, val) => { | |
acc[0] = acc[0] === undefined || val < acc[0] ? val : acc[0]; | |
acc[1] = acc[1] === undefined || val > acc[1] ? val : acc[1]; | |
console.log(acc); | |
return acc; | |
}, []); | |
} | |
console.log(minMax([ 3, 98, 1, 6, 100 ])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment