Return Largest Numbers in Arrays

Hello guys, please am trying to write a JavaScript program that returns the largest Numbers in Arrays of numbers but stuck somewhere.
I found out that the program passes only for +ve numbers but fails for -ve numbers. Below is the program; kindly give me your support:

function largestOfFour(arr) {
  let largestNum = [0,0,0,0];
  for(let arrayIndex = 0; arrayIndex < arr.length; arrayIndex++)
  {
    for(let subarrayIndex = 0; subarrayIndex < arr[arrayIndex].length; subarrayIndex++){
    if (arr[arrayIndex][subarrayIndex] > largestNum[arrayIndex])
    {largestNum[arrayIndex] = arr[arrayIndex][subarrayIndex];
    }
  } 
  } 
  return - largestNum;
}

It helps if you provide a link to the challenge.

Here you assume the largest will be at least 0.

I have sent the link above. Does it help?

Is this a valid assumption?

I assumed that this is an array that will contain the result of the 4 sub-arrays after iteration.

But here you compare to the initial values, which are all 0

Yes. But is that not correct? please help.

don’t you get a syntax error?

@ILM I actually got a syntax error here but have corrected it.

Guys I have finally gotten it right. Thanks for your supports

Here is the latest code which passed:

function largestOfFour(arr) {
var largestNum = ;
for(var arrayIndex = 0; arrayIndex < arr.length; arrayIndex++)
{
var max = arr[arrayIndex][0];
for(var subarrayIndex = 0; subarrayIndex < arr[arrayIndex].length; subarrayIndex++){
if (arr[arrayIndex][subarrayIndex] > max)
{ max = arr[arrayIndex][subarrayIndex];
largestNum[arrayIndex] = max ;
}
else{
largestNum[arrayIndex] = max ;
}
}
}
return largestNum;
}

Good work getting it passing!

Note that var is a legacy feature that shouldn’t be used!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.