# Question on style

This may sound like an odd one, but I’ve completed this unit and I was very happy to have done so at the first time of trying, my thought process was to loop through the sub arrays and sort the elements into ascending order, from there just pop off the end of the array and bosh, sorted.

However, after I’ve finished the until I looked at the examples in the get a hint section and now I’m wondering if I’ve missed the point of this unit completely or have I just completed this unit the way that I currently know how to solve a problem in my mind?

A developer once told me, “it doesn’t matter as long as you get it to work, then you can go about making it better” I assume that might apply here but was hoping to get some feedback on the style of the completed outcome.

``````
function ascendingOrder(a, b){
return a - b;
}

function largestOfFour(arr) {
let newArr = [];

for(let i = 0; i < arr.length; i++){
newArr.push(arr[i].sort(ascendingOrder).pop())

}

return newArr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15`.

Challenge: Return Largest Numbers in Arrays

You need to find the largest number inside of each of sub-arrays. Even though `.sort()` does the job, it also finds the second largest, the third largest and so on, which is unnecessary CPU cycles.

1 Like

I like how you named the sort comparator, it reads very nicely inside that largestOfFour function. Using sort is clever, but you might want Math.max instead which is O(n) instead of O(n log n).

Whenever you find yourself pushing to a new array in a loop the way your largestOfFour function does, you can usually replace it with arr.map()

1 Like

Ah yes, if this was a large array with thousands of subarrays then this could prove to be an issue pretty quickly, I didn’t think of it in that way, thanks for the reply.

Whenever you find yourself pushing to a new array in a loop the way your largestOfFour function does, you can usually replace it with arr.map()

Thanks, I don’t think I’ve touched on the .map() method as yet?

It’s definitely something that didn’t spring to mind when I was completing the unit, I’m going to go back and give it another go using that method.

Thanks for the input, much appreciated.