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.

Your code so far

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 information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:
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.

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!