# Return Largest Numbers in Arrays, any help?

Tell us what’s happening:
Why this code doesn’t work.
Just a note: please, don’t give the whole code. Just tell me why this code doesn’t work and what is the solution. That’s all. I tried another code before but this code specially just didn’t work and I don’t understand why.

``````function largestOfFour(arr) {
// You can do this!
var finalarray = [];
for (var i = 0; i < arr.length;i++) {
arr[i].sort(function (a,b) {
return a - b;
});}
for (var j = 0; j < arr[i].length;i++) {
var array = arr[i].pop();
finalarray = finalarray.push(array);
}
return finalarray;
}

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; rv:60.0) Gecko/20100101 Firefox/60.0`.

https://www.freecodecamp.org/challenges/return-largest-numbers-in-arrays

Every line in the above code segment is either using the wrong variable name You need to look line by line and make sure which variable names you should actually be using.

Then, in the following line of the same for loop, you need to review the documentation for the push function and what value it returns.

``````    finalarray = finalarray.push(array);
``````

Hint: You are assigning a number to finalarray instead of an array.

1 Like

Any help in finalarray = finalarray.push(array) one?

I tried to return an array but I get numbers.

Do you understand why the above line of code assigns a number to finalarray? Since push affects the array directly, there is no need to assign it to anything. Once you push an new element into finalarray, then finalarray will have the all the elements it had before, plus the new element at the end.

You should change the name of the array variable, because it actually will contain a number, because the pop function you used returns the element which is “popped” off the end. In this case, you are popping off the largest number in the subarray.

1 Like

I just knew how to do it now.

I replaced the line by finalarray.push(arr[j].pop());

I pushed the value directly of arr[j].pop(); to finalarray which resulted in an array.

It just seems that I forgot to include push(). Wow, it’s surprising how forgetting something leads to bigger problems in programming.

Thank you for help!