Can someone explain?

Tell us what’s happening:
Can someone explain to me why doesn’t “let biggestNum = 0” work. Is it due to the nested array? I have used it with simple arrays and it worked while here it doesn’t pass the last test.

Your code so far


function largestOfFour(arr) {
  // You can do this!
  let numArray = [];
  
  for (let i = 0; i < arr.length; i++) {
    let biggestNum = arr[i][0]; 
    // why doesn't  "let biggestNum = 0" work
    // is it because it is a nested array?
    for (let j = 0; j < arr[i].length; j++){
      if (arr[i][j] > biggestNum) {
        biggestNum = arr[i][j];
        }
    }
  numArray.push(biggestNum);
  }
  return numArray;
}

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


Your browser information:

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

Link to the challenge:

One of the subarrays in the last test input consists only of negative numbers. Naturally the biggest of those is negative, but it can’t challenge the initial assumption that the biggest number might be 0.

let biggestNum = 0 would work if all of the subarrays have at least one integer greater than or equal to 0.

1 Like

Oh thanks. I didn’t see that all were negative. So essentially arr[i][0]. The zero refers to the index of the subArray which we loop through?