Return largest number in arrays.. don't know what went wrong

Return largest number in arrays.. don't know what went wrong
0.0 0

#1

Can’t see what’s going wrong with this… it returns the correct numbers, can anyone help me out?

Your code so far

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

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:


#2

highest is going to be the largest number found in the whole arr. You need to find the highest in each sub array.


#3

The current code already returns the largest number from each sub-array, that’s is why I don’t understand what is going wrong.


#4

For one, this statement here is incorrect:

for(j=0; j<arr[i][j]; j++){

I’m guessing that you want to loop through the size of each sub-array. The condition needs to be modified. The same idea you used with arr.length in the first loop is the key.

Other than that, it should work.


#5

For the array of largestOfFour, the next sub-array is larger than prev-array.
for(i=0; i<arr.length; i++){
highest = arr[i][0];
for(j=0; j<arr[i][j]; j++){
if(arr[i][j]>=highest){
highest = arr[i][j];
finalArr[i] = highest;
}
}
}