Created
September 14, 2023 14:52
-
-
Save mflorida/7c3e6f05f711101886037705e1e81996 to your computer and use it in GitHub Desktop.
Find min and max in array of values.
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
// 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