I am trying to return the larges numbers within a nested array

I am trying to return to larges numbers within a nested array, my solution is only correct if the largest number in a preceding array is smaller than the largest number in the next sub array, otherwise it fail

Your code so far


function largestOfFour(arr) {
let biggestInArr=0;
let result = '';
let newArr = [];
for(let i=0; i<arr.length; i++){
 for(let j=0; j<arr[i].length; j++){
   biggestInArr = arr[i][j]>biggestInArr ? arr[i][j] : biggestInArr;
     //console.log(biggestInArr);
 }
 newArr.push(biggestInArr);
 //console.log(newArr);
 //console.log([biggestInArr])
 // } 

}

return 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 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

I see two problems.

  1. The placement of your creation of let biggestInArr=0; - you are creating it outside of the loops so it is always the same variable being used - you either need to find a place to put it so that it will be recreated once for each of the relevant iterations, or you are going to have to reinitialize it each time.

  2. You are initializing your biggestInArr to 0. How will this work with negative numbers? There are two ways to handle this. Either set your initialization to a number that is so small that no other number can be smaller. JS has constants for both the smallest number the system can handle and for the smallest number theoretically imaginable. The other option would be to initialize your variable to something like null. The problem is that your comparison will always be false for null so you would have to invert the logic in your ternary.

When I fix those things, the code passes for me.

there are three ways I would say: you set it to a number in the array, and you just compare it to the other numbers in the array, reducing by one iteration also

i did all you suggested, and the output was [undefined, undefined, undefined, undefined].

 biggestInArr === undefined ?  arr[i][j] : arr[i][j]>biggestInArr ? arr[i][j] : biggestInArr;

i tried the above still yet, it didnt work.

what’s your whole code?, as it is your snipped does nothing


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

function largestOfFour(arr) {

  

  let result = '';

  let newArr = [];

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

    let biggestInArr;

   for(let j=0; j<arr[i].length; j++){

     biggestInArr === undefined ?  arr[i][j] : arr[i][j]>biggestInArr ? arr[i][j] : biggestInArr;

       //console.log(biggestInArr);

   }

   newArr.push(biggestInArr);

   console.log(newArr);

   //console.log([biggestInArr])

   // } 

  

  }

  

  return newArr;

}

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

you never change the value of biggestInArr, so it’s undefined as determined by the line in which it is declared


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

sorry for not formating my code. i made the necessary changes, and it now works.

  
  let result = '';
  let newArr = [];
  for(let i=0; i<arr.length; i++){
    let biggestInArr;
   for(let j=0; j<arr[i].length; j++){
     biggestInArr = biggestInArr === undefined ?  arr[i][j] : arr[i][j]>biggestInArr ? arr[i][j] : biggestInArr;
       //console.log(biggestInArr);
   }
   newArr.push(biggestInArr);
   console.log(newArr);
   //console.log([biggestInArr])
   // } 
  
  }
  
  return newArr;
}

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

NOTE: This solution will only work for shallow arrays (one nested level )

yes. is there a particular solution that can handle all case? i will be happy to see

If you don’t know beforehand how deeply nested your arrays will be, that would be a prime example for using recursion.

1 Like

Hey @CodebanK!

Congrats on solving the problem! If you are interested in an alternative solution only using one for loop you can look at this one with comments explaining code.

//Math.max returns the largest number of each sub array
function largestOfFour(arr) {
  let newArr = []
  for(let i=0; i<arr.length; i++){
   newArr.push(Math.max(...arr[i])) // this is spread syntax. 
// Copies the sub array and find max of each sub array and push results to newArr. 
  }
  return newArr;
}

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

As you can see there are many ways to solve this problem.

Keep up the good work!

1 Like

thank you, i appreciate your solution

1 Like

Also…consider the expected behavior for deeply nested arrays…do you remove arrays and add them to the top level? Do you flatten the array? What do you do in your recursion ? Just food for thought. :slight_smile:

1 Like

You still need to do a size comparison. But you have exploit the fact that comparing to an undefined will always return false. So, for the false case, you want to set your variable to the currently tested value from the array.

isn’t that what happens with biggestInArr === undefined ? arr[i][j] : ... ?
when biggestInArr is undefined it returns arr[i][j]

Oh, sorry, I missed that it is a compound ternary. I’m not a big fan of compound ternaries, but let me take a look,

Oh, I’m sorry, I missed the part that you had a working solution … carry on.

2 Likes