Return Largest Number in Array

Dear Members,

Please check my code is not running.

function largestOfFour(arr) {
let result = 0;
let maxLength = 0;
for (let i = 0 ; i<arr.length ; i++)
{
for(let j = 0 ; j < arr[i].length ; j++)
{
if (arr[i][j] > maxLength ){
result = arr[i][j];
}
}
}

return result;
}

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

Hello
you need to store the max of each subarray in a resultArray

Hello bro,

But I am storing the value in result.
result = arr[i][j]

Ahh I see… means like that.

result = max[j]

can i see the freeCodeCamp link of the challenge

Here it is: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays/

No need for fancy loops and such. Turn your array into string and extract and sort numbers:

let arr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1], [-101, -78]];
let str = ""+arr;
arr = (str.split(","));
arr = arr.map((item,index)=> Number(item));
arr.sort(function(a, b){return b - a});    
console.log(arr);

@codename11 bro, cannot understand the code. But, it seems that code is only valid for the given arr.


It’s good. but i think it’s because they asked to use loops. And tests are probably checking for loop use somehow …

@codename11 first please first point out mistakes in my given code, once challenge will get solved and my concept will get clear. I will also check your advance method.

function largestOfFour(arr) {
let result = [] ;
for (let i = 0 ; i < arr.length ; i++){
    let max = arr[i][0];
    for(let j = 0 ; j < arr[i].length ; j++){
      if (arr[i][j] > max ){
          max = arr[i][j]  ;
      }
    }
    result.push(max);
  }
  return result;
}
1 Like

let max = arr[i][0]; is the initial value to the max element in each subarray, if you make it 0 it will not work for subarray with all elements are negative.

and this result.push(max) ; is : to push the max of each subarray one by one to the result array.

1 Like

@oussama-jlassi what’s the need for

let max = arr[i][0];
and 
result.push(max);

see the previous reply

Yes checked. Thank you very much :slight_smile:

1 Like

@oussama-jlassi brother didn’t get it yet why we assume,

let max = arr[i][0];

what if we declare in beginning like let max = 0;

Hi
Look at this case of sub-array [-2, -3, -4] the max is -2 but it is less than max=0,
so you will end up by 0 which is wrong.
so we assume that the max is the first element of the sub-array : arr[i][0], or any other
element of the sub-array, but we are not sure if our sub-array passed as argument to our function contain more than one element.

Sub Array is j right. we had assign an value max = [i][0] so it will not consider -1 or 2 or other. Is it like that?

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
[4,5,1,3] : is called sub-array because it is inside an other array.
why we choose arr[i][0] as initial value for the max , look at this link :