# Return Largest Numbers in Array - am I close?

Hi guys,

I’ve put together some code that makes sense in my head… yet it doesn’t give me the desired output, only the largest number from the first array item. What am I missing here?

``````function largestOfFour(arr) {
// You can do this!
// for loop to iterate through 0 to length of array, i.e. 4.

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

// sort each array item from low to high

arr[i] = arr[i].sort();

// if nothing is defined in the brackets, it sorts by each characters Unicode Point Code value.
// otherwise if you want to sort from low to high, use (a, b) or high to low, use (b, a)

// pop the character at the end of arr[i] and equal it to arr[i]
var newArr = [];
newArr[i] = arr[i].pop();
arr = newArr;
}

// end of for loop

return arr;
}

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

It´s pretty close
Think about what happens when you assign an empty array to newArr inside the loop

On a side note, it should be `for (var i = 0;...)` and you don’t really need newArr.

1 Like

why not to use two loops going through all the arrays (4 in this case) and finding the largest number? It will not be longer than with sort because sort() is time consuming it too…

It seemed obvious after you pointed it out… I’m one step closer, but my output now gives [5,27,39,857] - the last number is wrong for some reason… I can’t see why but I’m guessing it has something to do with the sort function?

``````function largestOfFour(arr) {
// You can do this!
// for loop to iterate through 0 to length of array, i.e. 4.
var newArr = [];

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

// sort each array item from high to low

arr[i] = arr[i].sort();

// if nothing is defined in the brackets, it sorts by each characters Unicode Point Code value. Otherwise if you want to
// otherwise if you want to sort from low to high, use (a, b) or high to low, use (b, a)

// select the charAt(0) from arr and = it to arr[i][j] or whatever it is
newArr[i] = arr[i].pop();
}

// end for loop

return newArr;
}

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

Never mind… by default the sort() function sorts by Unicode value, and in Unicode it seems that 857 is higher than 1001. Thanks for your help!

``````function largestOfFour(arr) {
// You can do this!
// for loop to iterate through 0 to length of array, i.e. 4.
var newArr = [];

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

// sort each array item from high to low

arr[i] = arr[i].sort(function(a, b){return a-b;});

// if nothing is defined in the brackets, it sorts by each characters Unicode Point Code value. Otherwise if you want to
// otherwise if you want to sort from low to high, use (a, b) or high to low, use (b, a)

// select the charAt(0) from arr and = it to arr[i][j] or whatever it is
newArr[i] = arr[i].pop();
}

// end for loop

return newArr;
}

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

You are very welcome!