Nice post! It’s interesting if not ironic that, because I’ve been actively leveling up myself in terms of algorithms and data structures, I was immediately able to recognize that your first example could be much more readable and succinct if it took advantage of the fact that Math.max
will work fine with nulls.
So…
if (maxValue === null || maxValue < myArray[i]) {
maxValue = myArray[i];
}
can become simply:
maxValue = Math.max(maxValue, myArray[i])
This pattern turns out to show up in many many “find max/min/sum” types of algorithm problems that come up in interviews, leetcode, etc., and so it seems apropos to mention here.