Basic Algorithm Scripting

Return Largest Numbers in Arrays

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].

function largestOfFour(arr) {
return arr;
}
largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]).

Someon help, I’m having an issue with finding the largest number among the negative values in the subarray.

Let’s start with simply one array:

[-72, -3, -17, -1]

And in particular, one value in that array: -72. When we first encounter that array, and its first value, what can we say definitely about that one value in comparison to any other we’ve encountered so far?

It is unique, in that it is the largest and the smallest value we have yet to find.

So what about this? Rather than start with some value that isn’t even in the array (we could, for example, start each array comparing to -Infinity, the least possible integer), why not start by setting our first value as our max value, and start comparing from the second one?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.