Programming

Another Look at Finding Minimum and Maximum in Arrays

I recently saw an exercise for finding a minimum and maximum value in an array. The wording of the question, which I won’t repeat here, hinted you to do that with a bit story behind it. Then, I reminisced about the good old days and attacking the problem with a simple for loop. You know, the one with a variable defined above the for loop and you keep checking the next value in the for loop against this variable so you could update it if necessary. Luckily, we now have array methods such as forEach, map, filter etc.

Filter and map can’t help us here since we are only interested in one final value than shrinking the said array or converting the array values. ForEach is no different than using plain old for loop really.  How about reduce?

Continue reading

Programming

Sorting An Array of Objects

This post has been on my mind for a while. Today, I would like to talk about sorting an array of objects. This exercise assumes your array is uniform, meaning all elements in the array are of the same type. To keep things more simple, I’ll assume you know what you are doing and we won’t check whether the array is uniform.

Additionally, we’ll keep this functionality as part of Array prototype so you can keep using it as if it is a native method rather than passing your array as an argument. One last note before we dive into the code is that while we are implementing our solution, we may as well throw in a way to sort the array ascendingly or descendingly.

Continue reading