I’m trying to find a solution based on what I’ve learned so far. The following works, but I’m curious as to why this works on only four sub-arrays? If I add another array (which I’ve done here) the function does not apply to it. Can someone please explain why?
function largestOfFour(arr) {
let newArr = []
for (let i = 0; i < arr.length; i++){
for (let j = 0; j < arr[i].length; j++){
newArr.push(Math.max(...arr[j]));
}
return newArr;
}
}
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1], [7, 8, 9, 10]]));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.
What do you mean by ‘doesn’t log the fifth array’? What output do you get?
Ah, I see it. You are pushing max(...arr[j]) and then you are returning inside of the i loop. Your logic is a bit mixed up. You don’t want to return inside of a loop in this case!
And with that j loop, you are going to check as many arrays as arr[i].length, which is different that arr.length.