Algorithm Scripting easy level

Hey all. I’m sure this has been answered but I don’t want to just look up an answer. I was hoping some kind soul will walk me through this to help me learn!

I am currently working on these algorithm scripting exercises, and like many. I feel like the cirriculum went from 0-100 quick and i’m just lost. I am trying to get the largest within the array of arrays in this problem. I am getting NaN and am confused as to why.

function largestOfFour(arr) {
  let maxArray = []
  for(let i = 0; i < arr.length; i++){
      console.log(arr[i]);
      maxArray.push(Math.max(arr[i]));
  }
  return maxArray; 
}

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

My thought process is, I do a for loop to go through each item of these arrays, and then use Math.max through each index of the array and push that Max number into a new array of it’s own and return the result. What would be the most logical step to get better at this insane algorithm scripting? Thank you guys !

Look at the documentation for Math.max:

It doesn’t take an array as an argument, the numbers need to be passed in as seperate arguments (the docs show a solution for this)

@DanCouper thanks man. Okay so I use the spread operator which shows the max of all the arrays, but the challenge is asking me to I guess pick the largest array out of all the arrays? If I understand this challenge correctly. Thanks for the help thus far.

Nono, so it’s asking you to return an array made up of the highest number for each of the sub-arrays. So if you have

[
  [1,2,3],
  [4,5,6],
  [7,8,9],
  [10,11,12],
]

You would return

[3, 6, 9, 12]

You don’t need to find the overall maximum – you only need to make one small addition to your existing code (fix that Math.max call by using the spread operator to turn an array of numbers into individual arguments)

Not quite sure I understand. If I use spread I get a giant array of all indices of the array of arrays:

function largestOfFour(arr) {
  let maxArray = []
  for(let i = 0; i < arr.length; i++){
      console.log(arr[i]);
        maxArray.push(...arr[i])
      }
      return maxArray;
  }
   

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

well, you do not have Math.max anymore

you are giving multiple arguments to push, and the method is adding them all as you said to do