Hello ! Let me comment the code and then propose my solution
arr.sort(function(a,b){
return (a-b);
})
This doesn’t make much sense right now. Look at the argument you are given : an array containing a bunch of number arrays.
when calling arr.sort this way, you are trying to sort the containers but by using “a - b” you are working with numbers. This function fails because you are working with the arrays and not the numbers inside.
To fix this, you need to iterate through the arrays and work with the actual numbers. This can be done either with a for loop or Array.prototype.forEach
for (i=0;i for(j=0;j arr[i].sort(function(a,b){
This line just doesn’t work. You can’t embed another for loop this way. Also notice that you aren’t incrementing “i” anywhere, so this just won’t go. The correct syntax for a for loop is :
for (var i = 0; i < arr.length; i++) {
// magic happens here
}
On a sidenote, you have to ask yourself if something’s wrong with this : You call .sort two times, and in the end you are reversing the return result.
Arr only needs to be sorted once.
SPOILER ALERT
Here is my solution :
function largestOfFour(arr) {
var sorted = [];
arr.forEach( function( item ) {
sorted.push( item.sort( function( a,b ) { return b > a; } ) [0] );
});
return sorted;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
arr.forEach permits me to work with each array inside, as “item”
item.sort will sort each array from the highest to lowest ( b > a )
We then push the index 0, which is the highest in the array sorted.
Good luck !