Return Largest Numbers in Arrays (works but....)

My code works, but my spidey-sense is telling me that in a more complicated scenario it would lead to problems. What those problems are, I’m not sure of. I was just hoping someone really smart could tell me why it’s a bad idea, despite being functional.

function largestOfFour(arr) {
let maxArray = [arr[0][0], arr[1][0], arr[2][0], arr[3][0]];

for (let i=0; i<arr.length; i++){
for (let j=0; j<arr[i].length; j++){
if(arr[i][j] > maxArray[i]){
maxArray[i]=arr[i][j];
}
}
}
return maxArray;
}

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

The only issue with your solution that it’s tailored to a specific case of four nested arrays (non-reusable). Ask yourself 2 questions:

  1. How do I find largest number in the array of numbers?
  2. How do I loop through the array applying first question to each item inside?

Good luck! :slight_smile:

1 Like

The non-reusable bit is what was kicking off my spidey senses - thanks!