Skip to content

Instantly share code, notes, and snippets.

@RickJP
Created April 10, 2019 04:22
Show Gist options
  • Save RickJP/c9c88f943997327eea875eb060af10a0 to your computer and use it in GitHub Desktop.
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)
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