Build the Largest Number Finder - Build the Largest Number Finder

Tell us what’s happening:

Hello, i finished the lab, all the tests aree fine and passed, but I am not sure, is it hardcoding or not (if i understand correctly, hard coding is when you code only for passing the test) help me please

Your code so far

function largestOfAll(arr){
   let largest1 = arr[0][0];
   for (let i = 0; i < arr[0].length; i++){
     if (arr[0][i] > largest1) {
       largest1 = arr[0][i];
     }
   }
   let largest2 = arr[1][0];
   for (let m = 0; m < arr[1].length; m++){
     if (arr[1][m] > largest2){
       largest2 = arr[1][m];
     }
   }
   let largest3 = arr[2][0];
   for (let n = 0; n < arr[2].length; n++){
     if (arr[2][n] > largest3){
       largest3 = arr[2][n];
     }
   }
   let largest4 = arr[3][0];
   for (let y = 0; y < arr[3].length; y++){
     if (arr[3][y] > largest4){
       largest4 = arr[3][y];
     }
   }
   let array = new Array(largest1, largest2, largest3, largest4);
   return array
}


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


// let arr = [3, 6, 2, 56, 32, 5, 89, 32];
// let largest = arr[0];

// for (let i = 0; i < arr.length; i++) {
//   if (arr[i] > largest ) {
//    largest = arr[i];
//   }
// }
// console.log(largest);


Your browser information:

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

Challenge Information:

Build the Largest Number Finder - Build the Largest Number Finder

if your function is passed an array with 3 or 5 sub-arrays your function would not work

yould you be able to make a more general function?

like that what i was thinking about, i will try to make it more flexible but i dont really know how to be honest, i am kinda sure it should work with loop first and then functions inside that loop or something like that, i will try again

consider this, is there a part of your code that is almost exactly the same repeated multiple times?

yes, the whole function repeates couple of times, i probably need to create a one single loop, i think i even have an idea how to do that, but i need a bit of time, as soon as i am ready with the new code, i will reply or maybe even create a new topic. Thanks alot!

Please definitely reply instead of making a new post. It makes it much easier to help you

1 Like

As a beginner, goodjob solving this on your own. Base from your code, i can say that it is a brute force solution, meaning you are literally storing and checking each array which is very hard to understand and quite ineficient. If you want the same approach but easier syntax and much more easy to understand code, here’s my solution

removed

Solution can be as simple as this. First the variable const array hold an array from the array2D, now then get the maximum number from that array then move on to the next.
Also, this solution only works because the size of each array in array2D is 4. It will be a different case if the arrays have different lengths.

1 Like

Please do not share solution code on the forum thanks