Strangely works for 3/4 arrays

So I wrote:

function largestOfFour(arr) {

for (var i=0; i<arr.length; i++) {
    var biggest = arr[i][0]; 
  for (var j=0; j<arr[i].length; j++) {
    if(arr[i][j] > biggest) {
      biggest = arr[i][j]; 
      console.log(biggest);  
    }
  }
}

  return arr;
}

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

It prints out 5, 27, 35, 37, 39, 1001 so it worked for the first two sub-arrays and the last one, but not the third one.

Why are you returning arr?

these comes from the third array

with this it starts

the value of biggest starts at 32

then there is 35, this is now the value of biggest, and it is printed
then there is 37, this is now the value of biggest because it is bigger than 35 and it is printed
same for 39
it does exactly what you have written it to do

you are not doing anything with the biggest values of the arrays as you are required to do instead

1 Like

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