DEV Community

Discussion on: Explain This Like I'm Five

Collapse
 
lamaalrajih profile image
Lama Al Rajih

The problem:

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].

My solution:

function largestOfFour(arr) {
for (i = 0; i < arr.length; i++) {
var largest = arr[i][0];
for (j = 0; j < arr[i].length; j++) {
if (arr[i][j] > largest) {
return arr[i][j];
}
}
}

Why do I need to have an empty bracket set for largestNumberArray, per your example? I'm not exactly clear on that part.