# Largest Numbers in Sub Arrays

Largest Numbers in Sub Arrays
0

#1

*Tell us what’s happening:
Good morning Everyone,

I’m stuck on this algorithm. I wanted to solve it by using a for loop to sort out the sub arrays and use the pop function to take the biggest number at the end of each sub array. When I use the sort function the first 3 sub arrays sort but the last last one does not. Am i doing something wrong. I am open to ideas of solving this a different way.

Thank you in Advance for all the help

Ray

``````function largestOfFour(arr) {
// You can do this!
var sort = [];

for (var i = 0; i < arr.length; i++){
sort.push(arr[i].sort());
}
return sort;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
``````

Your Browser User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36`.

#2

The array is sorting, but when you use the sort function without any parameters, the elements are treated as strings instead of numbers. Read the following threads for some good information on how to use sort for this challenge.

#3

Thanks again Randell! sorry asking another noob question.

here is my solution and it returns the largest numbers in each subarray but its not letting me complete the algorithm. I cannot figure out what i’m doing wrong or missing.

``````function largestOfFour(arr) {
// You can do this!

for (var i = 0; i < arr.length; i++){

arr[i].sort(function(a, b){
return a<b;

}) -(arr[i].splice(1));

}
return arr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
``````

#4

The problem is you are returning an array of arrays (each sub array does contain the largest number), instead of an array of the largest numbers of each array.

You return:

[ [ 5 ], [ 27 ], [ 39 ], [ 1001 ] ]

when you should return:

[ 5, 27, 39, 1001 ]

See the difference?

When you use arr[i].slice(1), you are removing the elements of each sub array, but not getting the value out of the sub array. How do we access the first element of an array in general? We reference the 0 index of it. I am not sure why you added a dash and surrounded the arr[i].splice(1) with ( and ), but that does not do anything different than just arr[i].splice(1).
So after you cut off the other elements in the sub array (as your code already does), you could simply replace the current sub array (arr[i]) with the first element of the sub array like:

``````arr[i] = arr[i][0];
``````

FYI - I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

#6

Thank you for giving me hints and nudging me in the right direction! I hope to one day contribute as you do. Sorry about not formatting the code.

below is the final answer and it works :).

``````

function largestOfFour(arr) {
// You can do this!

for (var i = 0; i < arr.length; i++){

arr[i].sort(function(a, b){
return a<b;

});
arr[i].splice(1);
arr[i]=arr[i][0];

}

return arr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

``````