Return Largest Numbers in Arrays - what am I missing?

Return Largest Numbers in Arrays - what am I missing?
0.0 0

#1

Hey everyone, I am solving this FCC algorithm and I can’t find what I am doing wrong.
It should return 4 largest numbers from every array.
Someone, who see it?
Code:

function largestOfFour(arr) {
  var biggestNumbers = [];

  for (var arrayIndex=0; arrayIndex < 4; arrayIndex++) {
    var actualBiggest = 0;
    for (var subArrayIndex; subArrayIndex < 4; subArrayIndex ++) {
      if (arr[arrayIndex][subArrayIndex] > actualBiggest)
       actualBiggest = arr[arrayIndex][subArrayIndex];
    }
   biggestNumbers.push(actualBiggest);
  }
   return biggestNumbers;  
}

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

Thank you :slight_smile:


#2

You did not initialize subArrayIndex to anything, so it starts off as undefined by default and since undefined is not less than 4, the inner for loop never executes, so you push four zeros to biggestNumbers.


#3

:DDDDDD
I was struggling so for about an hour what’s wrong in my code and it is this?
That’s funny
Thank you Randell :slight_smile: