I'm just getting started on learning javascript, and I'm creating this simple program that takes the largest numbers from an array, and put them in a new array, which will be returned in the end.
The function is called largestOf(), and for example,
largestOf([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return [27,5,39,1001].
What I have so far is this, and I don't know how to fix it, or if it has something to do with the way I am utilizing the brackets.
function largestOf(arr) {
var nArr = [];
for (var i = 0; i < arr.length; i++) {
n = arr[i].length;
max = 0;
for(var j = 0; j < n; j ++) {
if (arr[i][j] > max) {
max = arr[i][j];
nArr.push(max);
}
}
}
return nArr;
}
What I am trying to do here is pretty straightforward. I'm running through every block in the array, picking the max, and putting that max in its own array (nArr) with the other max's.
I want to know how to fix what I have while still doing it my way.
Thank you
.push()call to outside the innerforloop.