Help: Return Largest Numbers in Arrays

Tell us what’s happening:

So far I’m able to return the largest number in the first array, but I don’t know how to make the loop continue through the remaining arrays.

One issue is the “[0]” (as in “arr[0][i]”) I have in place which only refers to the first array, is there a way I can increment this number so that the loop continues?

Is there a better approach I should take overall? Or is there a correction I should explore? A hint would be much appreciated.

Thank you.

Your code so far

function largestOfFour(arr) {
let newArr = [];
let holder = [];

for (let i=0; i < arr.length; i++){
  var tiger = Math.max(...newArr)

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

Try with a little pseudo-code:

- for each element of my array
 - for each element on the sub-array
  - pick the largest number

As you can see, even logically the easiest approach is a double loop, one on the “main” array, and one for each sub-array.

I won’t give you an example since that will spoil the solution too much, however think about the code you wrote:

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

Can you spot, besides the need of hard-coded 0 value, a flaw?
What happen if for example the input of my array would be something like

largestOfFour([ [1,3,4,6,15] ]) // a single array inside an array

Can you predict the outcome of your function?
Hope this will help.
Happy coding :sparkle:

1 Like

Thanks! I was able to get it to worked. I had tried a double for loop before, not sure why I didn’t stick with it…

function largestOfFour(arr) {
  let holder = [];
  for (let i=0; i < arr.length; i++){
    for (let j = 0; j < arr.length; j++){
     var tiger = Math.max(...arr[i]);


I’ll give you another approach since you asked for it.

//Comments in the function refer to the next line of code.
function largestOfFour(arr) {
  for (let i = 0; i < arr.length; i++) {
    //Grab the first array in the 'arr' argument and get its largest number.
    //Since you only need to refer to 'arrayMax' inside of this loop, you 
    //can use 'const' or 'let', but 'var' works too.
    const arrayMax = Math.max(...arr[i]);
    //Since you need to return 'arr' for this exercise, replace the array from
    //which you got 'arrayMax' with 'arrayMax'. Basically, you are getting rid
    //of the array 'i' because you don't need it after you get its largest number.
    //So you place 'arrayMax' in its place (its index).
    arr[i] = arrayMax;
  //Once the loop is done, all the arrays in 'arr' will have been replaced
  //by their largest number
  //return 'arr'
  return arr;
1 Like

Glad you can come up with a solution :slight_smile:

Just a question: what’s the purpose of that empty loop?

1 Like

Ah I see, I didn’t put it to use and it wasn’t needed. Thanks for highlighting this for me.