Trying to make sense of the JavaScript code
var arr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];
var main = [];
var long;
for(var i=0; i<arr.length; i++){
long = arr[i][0]; //assigning the first element of each array to the long variable
for(var j=0; j<arr.length; j++){
//comparing the long variable with dynamic array elements
if(arr[i][j] > long) {
long= arr[i][j];
}
}
main.push(long);
}
return main;
I've grasped most of it except the long = arr[i][0];
part.
I understand we're comparing arr[i][j]
with arr[i][0]
in the if
statement, but I'm unsure about how the computer is executing this comparison.
When I check arr[i][0]
, it gives me the first element of all arrays within the arr
array.
My assumption is that it's comparing each value with arr[i][j]
, but I'd appreciate clarification on this point.
Apologies if this is a basic question — I'm just starting out. Thank you in advance!