Hello I am stumped. I can not seem to figure out how to get the largest number from a sub array, when the sub array is made up of negative numbers. Can any one explain how to do that using for loops? Spoiler below if you haven’t finished this challenge.

```
var largestNum=[0,0,0,0]
for(let i= 0;i<arr.length;i++){
for(let j = 0;j<arr[i].length;j++){
if(arr[i][j]>largestNum[i]){
largestNum[i]=arr[i][j]
}
}
}
return largestNum;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); ```
```

sanity
April 1, 2021, 1:12pm
#2
As you have notice the initial largest number can’t be `0`

, if values in array are negative. Consider what else can be used instead of `0`

for that.

I attempted including an empty array but that didn’t work either.

sanity
April 1, 2021, 1:21pm
#4
There are at least two approaches

What value would be certainly smaller (or equal) than any possible value in array? Small hint could be to think about it from a math perspective.
Another possibility is to consider as temporary largest one of the values from array.

-infinity ? Is there a way to make the array filled with -infinity in javascript?

The project requires you to “RETURN the largest number” in a group of 2nd level arrays but not necessarily return “largestNum” number.

You have to “push” your largest number in a new array.

I was having trouble comparing values to the negative numbers in the array, In the top of the code I create an array of zeros, problem is when it compares negative numbers it does not assign a value to the last index.

sanity
April 1, 2021, 1:42pm
#8
Indeed there’s a way to use infinity concept in javascript. It can be accessed as `Infinity`

or `-Infinity`

More about it can be read at Infinity - JavaScript | MDN

Thanks Sanity! you really helped me understand how to solve my problem!

```
var largestNum=[-Infinity,-Infinity,-Infinity,-Infinity]
for(let i= 0;i<arr.length;i++){
for(let j = 0;j<arr[i].length;j++){
if(arr[i][j]>largestNum[i]){
largestNum[i]=arr[i][j]
}
}
}
return largestNum;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
```

system
closed
October 1, 2021, 1:47am
#10
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.