I've been trying to solve this practice problem:
Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
But my code only returns the single element of from the whole array, if I unshift all the max elements it produces totally wrong results, I tried to execute the nested loop separately and it worked fine but it creates a problem when combined with outer loop.
function largestOfFour(arr)
{
// You can do this!
var max = 0;
var largestArray =[];
for (var i = 0; i <4; i++)
{
for (var j = 0; j <4; j++)
{
if (arr[i][j]>max)
{
max=arr[i][j];
largestArray.unshift(max);
//console.log(max);
}
}
}
console.log(largestArray);
return max;
}
largestOfFour([[4, 5, 1, 13], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Math.maxanswers the OP would like help with the code they've written.