Return largest numbers in Arrays - needs better understand

Hello Campers and others experienced programmers, I need your help with something…

PS: Sorry for english, it’s not my national language

A few hours (3+) I was doing FCC challenge “Return largest numbers in Arrays”… I was trying a lot of ways and it just didn’t worked… I didn’t know what do I do wrong, so I just found this article - Three ways you can find the largest number in an array using JavaScript
Yes, I gave it up, I was really frustrated from everything. But I don’t want just copy and paste code, I just want to deeply understand how it works…

The code should looks like this:

function largestOfFour(arr) {
   var largestNumber = [0,0,0,0];
   for(var arrayIndex = 0; arrayIndex < arr.length; arrayIndex++) {
    for(var subArrayIndex = 0; subArrayIndex < arr[arrayIndex].length; subArrayIndex++) {
       if(arr[arrayIndex][subArrayIndex] > largestNumber[arrayIndex]) {         
          largestNumber[arrayIndex] = arr[arrayIndex][subArrayIndex];
        }
    }
 }
 return largestNumber;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

and because I wanted to know values of some variables and others, I wrote to that code console.log

    function largestOfFour(arr) {
       var largestNumber = [0,0,0,0];
       for(var arrayIndex = 0; arrayIndex < arr.length; arrayIndex++) {
        for(var subArrayIndex = 0; subArrayIndex < arr[arrayIndex].length; subArrayIndex++) {
           if(arr[arrayIndex][subArrayIndex] > largestNumber[arrayIndex]) {         
              largestNumber[arrayIndex] = arr[arrayIndex][subArrayIndex];
            }
        }
     }
      console.log (" arrayIndex is " + arrayIndex + "arr.length is " + arr.length + "subArrayIndex is " + subArrayIndex + "arr[arrayIndex].length is " + arr[arrayIndex].length + "arr[arrayIndex][subArrayIndex] is " + arr[arrayIndex][subArrayIndex] + "largestNumber[arrayIndex] is " + largestNumber[arrayIndex] + "arr[arrayIndex][subArrayIndex] is " + arr[arrayIndex][subArrayIndex]   );
    }
`largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);`

But here’s the error - - - TypeError: arr[arrayIndex] is undefined [Learn More]"

So the question is… is here some way how to get know the value of arr[arrayIndex]… or is it possible to write everything to the global variable, WITHOUT THE FUNCTION ?

like this:

var largestNumber = [0, 0, 0, 0];

var arr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];

ffor(arrayIndex = 0; arrayIndex < arr.length; arrayIndex++) {
    for(subArrayIndex = 0; subArrayIndex < arr[arrayIndex].length; subArrayIndex++) {
       if(arr[arrayIndex][subArrayIndex] > largestNumber[arrayIndex]) {         
          largestNumber[arrayIndex] = arr[arrayIndex][subArrayIndex];
        } console.log (" arrayIndex ===  + " + arrayIndex + " arr.length === + " + arr.length + " subArrayIndex ===  + " + subArrayIndex + " arr[arrayIndex].length === + " + arr[arrayIndex].length + " arr[arrayIndex][subArrayIndex] ===  + " + arr[arrayIndex][subArrayIndex] + " largestNumber[arrayIndex] === + " + largestNumber[arrayIndex] + " arr[arrayIndex][subArrayIndex] ===  + " + arr[arrayIndex][subArrayIndex]   );
    }
}

or it is wrong ?
Thank you, Lukáš Hanák, from Czech Republic.

You put your console.log() too low; move it one curly brace higher.

Regarding writing everything in global - your solution won’t pass the tests. And you shouldn’t be doing it anyway.

You should learn how to debug properly. I am happy to teach you.
This is another way - use http://pythontutor.com/visualize.html#mode=edit (for Javascript)

Immediately I see this problem:
ffor is not valid