Return Largest Numbers in Arrays - change the row of numbers into an array

Hello everyone,

in this task I managed to go that far. I’m not sure if you can see that, but the function logs correct numbers: 5, 27, 39, 1001.
The problem is: they are not an array. I tried different methods like Array.from() or includes() but either they are not suitable for this case or I don’t use them correctly. It feels like I miss one little step to the solution. Am I on a right track?

Your code so far

function largestOfFour(arr) {
  // You can do this!
 for (var i=0; i < arr.length; i++) {
    console.log(arr.concat(Math.max.apply(null, arr[i])));

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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:

From my understanding, you are currently concatenating (adding) the highest value from each array within array back to the array.

From my observations, you will probably need to create a temp array outside the loop and push the highest value from each array to that temp array (Math.max.apply(null, arr[i])). Once the loop is finished you can return that temp array. This means that you don’t need to use the concat method in this problem.

I hope that somehow helps.

1 Like

The concat function does not change the existing array called arr. The concat function returns a new array which you could assign to another variable.

1 Like

Thank you very much for your reply.
I realized I pasted a wrong code. This is the code that gives right numbers, but they are not an array. So I got to the point that you suggested, but I am not sure, I understood what you mean regarding the temp array. Could rephrase this?

function largestOfFour(arr) {
for (var i = 0; i < arr.length; i++) {
console.log(Math.max.apply(null, arr[i]));

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

Create a variable before loop which is initialized to an array.

Inside loop push to that array what you are doing with the console.log.

After the loop, return that array.

1 Like

thanks again. that worked indeed!