Return Largest Numbers in Arrays - help

Tell us what’s happening:
I was stuck so I looked at the solution and I changed my var modArr = to modArr = [0,0,0,0] and I wanted to use push(i) to push the largest integer to my modArr instead I did the solution way.

Was my way possible?

Your code so far

function largestOfFour(arr) {
  // You can do this!
  var modArr = [0,0,0,0];
  
  for(var i = 0; i<arr.length; i++){
    for(var j = 0; j<arr[i].length; j++){
      if(arr[i][j] > modArr[i]){
        modArr[i] = arr[i][j];
      }
    }
  }
  
  
  return modArr;
}

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; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:
https://www.freecodecamp.org/challenges/return-largest-numbers-in-arrays

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

What if you did not know the exact number of sub arrays ahead of time? In this challenge, you knew there would be only 4, which is why your var modArr = [0,0,0,0]; worked.

I did not pay attention to the 4 array limiter so I was thinking about an empty array from the beginning hence I had assigned the var modArr = [] and intended to push in values. Would that have been possible is my question.

Test it out for yourself. The best way to learn is to experiment.

I did experiment with the following before peeking at the solution

I had assigned var modArr = [] and used the following code inside the if statement

modArr.push(arr[i][j])

it returns an empty array.
Could you give me a hint or a nudge in the right direction?

where can I find info on other cool keywords like the spoiler one you just mentioned?

You are going to have to rethink your if statement (below):

if(arr[i][j] > modArr[i]){

modArr[i] will be undefined in each iteration of the inner for loop, so since arr[i][j] > undefined is false, nothing gets pushed into modArr.

My suggestion is to create a variable named largest which gets initialized as the first element of each subarray (before the inner for loop begins) and then your iterate through a given sub array and compare largest to arr[i][j]. If arr[i][j] is greater than largest, then assign it to largest. After the inner for loop completes, you know that largest contains the value of the largest element in the preceding sub array, so you can add it to modArr.

Thanks for the effort, I have currently moved on to other challenges but will try out what you have suggested and I will let you know the result.