Return Largest Numbers in Arrays- Way of thinking

So hi guys I am going throught the Algorythms certification and I am kinda stuck…
I have rapidly seen the solution from the get a hint option and I see that I have understood correctly the use of 2d ARRAY etc. SO… in order to not give in and see in a detailed manner the correct answer could you gimme a hand :wink:

function largestOfFour(arr) {
  var max;
  var b = new Array(); //max array
  for(let j=0;j<arr.lenght;j++){
    for(let i=0;i<arr[j][i].length;i++){
      if (max<arr[j][i]){ max=arr[j][i];}}
  return b;

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/69.0.3497.100 Safari/537.36 OPR/56.0.3051.99.

Link to the challenge:

1 Like

So, forget everything you know about coding, and remember how to speak. I am not sure if English is your first language (“le fou”), but we’ll have to get along in English. Worry about the code last. Ask yourself what PROCEDURE you would use to accomplish the task yourself (not instructing a computer) FIRST:
So, let’s look at the sample data you provided:

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

Let’s say you wanted to solve the problem yourself:

Return an array consisting of the largest number from each provided sub-array.

How would you go about it, provided two constraints: You can write down any one number you want on paper, and any one yes/no value on paper, and you are only fed each sub-array one number at a time. I mean: you would be given 4, then 5, then 1, then 3, and asked to pick the largest of the series.

Given the constraints I placed on you, would you be able to tell if 5 was the biggest or smallest so far by the time you got to it? If not, what would you need to have written down? Start from there, and your whole life in computing will be easier. I promise.

1 Like

Hello and thnx for the answer.
Sooooo from what I understand the problem lies on my max variable as it doesn’t ‘‘store’’-take under consideration the previous value of each element …
I shall think about tempering a bit with it.

From what I see and understand I correctly loop through the array’s elements so that’s where the problem has to be ( i hope :smiley: )

And I have found the problem with my code…
I wasn’t going through the elements 1,2,3,4 of the 1st,2nd… array
But I way actually searching for for the length(which I also had a typo in my code) of a single element eg at first execution: arr[j][i]=>arr[0]][0]=4 which is invalid…

To say the truth, I kinda understood my mistake via console.log everywhere, but thnx for the overall comment/way of thinking and of course pointing out that the problem was while ‘‘reading’’ the elements of an array :wink:

1 Like