Return Largest Numbers in Arrays - Is my attempt anywhere close

This challenge is similar to the one before it the difference being doing it with multi dimenstions as supposed to just one.

Hence, I tried emulating a similar approach - doesn’t solve the challenge, can someone give me some pointers please?

Your code so far




function largestOfFour(arr) {
  // You can do this!


var largestNumber = 0;

for (var row = 0 ; row < arr.length ; row++) {
for ( var col = 0 ; col>arr[row].length; col++) {

 arr[row].split('');

if ( arr[row][col] > largestNumber) {

  largestNumber = arr[row][col];
}

}


}

return largestNumber;

}

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/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays

You appear to be returning a single number. What kind of return value is the challenge expecting?

What does arr[row].split(''); this do for you?

How sure are you of those test expressions in your for loops?

That test for largest, will it work in all cases?

I have did it today. Im afraid to place code here because my last code was locked and hidden by admin. But It can be done with one line of code by using .map and Math.max.apply

Thank you all and especially this pointer.

Seems like my appraoch was wrong and I cannot do it with what I’ve learnt so far. Math.max.apply is new to me so back to googling I go

Task is very simple even it looks complicated and can be done multiple ways. You can use what you already know, just use logic. All you need is to loop thru each array. In my code I had used .map(), as you know map() is executing given function to each element of array. Math.max() return biggest number. Its’ simplest but NOT only solution.

You’ve got this. If you have made it this far in the challenges then you should have all you need to solve this one. Your first solution attempt was leading you in the right direction.

You can do this with for loops and some simple check for largest number.

This is a project to make your own map function so while .map() and Math.max() would make for a short solution I assure you that you can do it without either of them. Besides, if you don’t understand for…loops to make your own map then you couldn’t possibly understand what javascript .map() method is doing for you in the background.

Revisit this challenge to review nested loops
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops

A few more hints and some pseudo-code to help you on your way

  • If you know how, use console.log() to see the values of your variables in your loop.
  • Before even attempting to test for largest make sure that your loop is visiting each element in each array. Does arr[row][col] always equal what you would expect each cycle of the loop?
  • Once your looping is good then develop a test for largest that works in all cases. What about [-10,-500,-1000]? Would your test successfully find the largest number in that array?
  • What do you do with largest when you find it? What kind of answer is the challenge expecting? Where do you store it?
loop through each subarray{  //[4,5,2,3] ,  then [13,27,18,26], ...
    loop through each element in subarray{  //  4,  5,  2,  then 3 ...
        largest = test if arr[row][col] is the largest so far
    }
        //Where do you store largest now that you have it? 
        //What kind of answer is the challenge expecting.
        save largest somehow
}

If you get stuck reach out again. Someone will help you.

1 Like

Thank you for such a detailed response that makes everything seem more simple.

I recently got back to this challenge after trying the others and have accomplished it but I do not think I should have passed it…

function largestOfFour(arr) {
  let largestNumInSubArr =[-999 , -999, - 999, -999];

for (var row =0 ; row<arr.length ; row++) {

for (var col = 0 ; col<arr[row].length; col++ ){
  



  if (arr[row][col] > largestNumInSubArr [row] ) {

    largestNumInSubArr[row] = arr[row][col];

   
  }
}

}

  return largestNumInSubArr;
}

The part I’m concerned about is of course

largestNumInSubArr =[-999 , -999, - 999, -999]

initially I set all values here equal to 0 but this leads to issues where if a sub array consists of negative numbers then of course the largest value for each index returned would each be zero.

So I set arbitrary large numbers which ‘‘solves’’ the challenge but leads to issues such as :

  • if each sub array size is greater than 4 , an array of corresponding index size would not be returned
  • if any element of each sub array is of a large negative number then finding the largest would fail

What I want to achieve with this is, an array which would have indexes equal to the amount of indexes of the arr passed into the function - I can do this with another for loop

However, I want each element for this array to initialize as ‘‘tending towards negative infinity’’ such that, it will be able to return the largest value no matter what…

Hope I’m making sense

OK - you tackled that looping like a boss.

The challenge did state that there would only be four but a more generic ‘catch-all’ solution would be better. Look into the array method push(). Using arr.push() your resultant array starts out as empty and you push the largest number from each subarray onto it. That would work whether you had four or four thousand sub-arrays.

There are a couple of ways around that you might be interested in.

Javascript has an Infinity keyword. And so also there is a -Infinity too that you could use as your first test instead of -999.
For example using [-72, -3, -17, -10]
start with -Infinity
-72 is > -Infinity, so now -72 becomes largest
-3 is > -72, so now -3 becomes the largest
-17 is not > -3, so -3 stays the largest
-10 is not > -3, so -3 stays the largest
no more to check so -3 must be the largest of the array - put that in your resultant array

Another solution would be instead of using -999 as your first candidate for largest just use the first number in the sub-array. For [4,5,2,3] start with 4 as your candidate for largest.
start with 4
4 is not > than 4, so 4 stays the largest (obviously you could skip this step)
5 is > 4, so now 5 becomes largest
2 is not > 5, so 5 stays the largest
3 is not > 5, so 5 stays the largest
no more to check so 5 would be the largest number of array

As stated earlier you could also use Math.max to either compare two numbers for largest or to get the largest of the entire sub-array at once but you’ll get to that later in the challenges.

function largestOfFour(arr) {
  var result = [];

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

    var largest = arr[i][0];  //using first in subarray

    for (var j = 1; j < arr[i].length; j++) {
      if (arr[i][j] > largest) {  // test for larger
        largest = arr[i][j];
      }
    }

    result.push(largest);
  }
  return result;
}
1 Like

Ah nice to know concept of infinity exists!

Thank you so much for your kind and detailed explanations:slightly_smiling_face: