Turns out this is simple. Well, it was simple anyway, but it's also quite concise.
I'll provide my code examples in javascript.
Let's suppose we have an array of number...
var myNumbers = [100, 50, 200];
We can use swanky destructuring assignment, which I know more commonly as the spread operator.
var myNumbers = [100, 50, 200];
var myMinNumber = Math.min(...myNumbers);
var myMaxNumber = Math.max(...myNumbers);
Short and sweet.
Top comments (6)
I think there's a typo, "nums" should be "myNumbers" :)
E.g.
Thank you!!! Fixed.
In case you want to be able to switch how you compare the items, you can also do something like this:
Nice snippet, I always forget about min and max :)
Wow nice!
excellent