Skip to content

Instantly share code, notes, and snippets.

@mflorida
Created September 14, 2023 14:52
Show Gist options
  • Save mflorida/7c3e6f05f711101886037705e1e81996 to your computer and use it in GitHub Desktop.
Save mflorida/7c3e6f05f711101886037705e1e81996 to your computer and use it in GitHub Desktop.
Find min and max in array of values.
// Return a tuple of the minimum and maximum values in `arr` array
function arrayMinMax(arr){
return [...arr].reduce(([min, max], value) => {
return [
value < min ? value : min,
value > max ? value : max
];
}, [Infinity, -Infinity]);
}
// Create a string of `len` copies of string `str`
function stringFill(len, str) {
return (new Array(len || 1000).fill(str || 'x').join(''));
}
// Modification of above function to find longest string in array of strings
function stringMinMax(strArr) {
return [...strArr].reduce(([short, long], str) => {
return [
str.length < short.length ? str : short,
str.length > long.length ? str : long
];
}, [stringFill(100), '']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment