I'm currently tackling a coding challenge that involves working with nested arrays. The task is to find the largest number in each sub-array and then create a new array containing only the largest numbers from each one. Initially, my approach was to define variables for each subarray, use a for-loop to compare values within each array, and then add the largest value to a new array. However, after implementing the first for-loop and testing my code, I noticed an unexpected result - the entire first subarray was being added to the new array. Before proceeding with additional loops, I want to identify the mistake in my code. Any insights into where I might be going wrong would be greatly appreciated. For context, this challenge is geared towards beginner JavaScript coders and it's recommended to utilize comparison operators in the solution.
function largestOfFour(arr) {
var one = arr[0];
var two = arr[1];
var three = arr[2];
var four = arr[3];
var newArr = [];
for (var i = 0; i < one.length; i++){
var oneLrg = 0;
if (one[i] > oneLrg){
oneLrg = one[i];
}
newArr.push(oneLrg);
}
return arr;
}
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])); //This test case returns [4,5,1,3] instead of just [5]