# Help : Return Largest Numbers in Arrays

Tell us what’s happening:

i can’t complete the challenge because my code fail to return -3 on the last test

`largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]])` should return `[25, 48, 21, -3]` .

``````
function largestOfFour(arr) {
// You can do this!
var largest = [];
for(var i = 0; i < arr.length; i++){
var v = 0;
for(var j = 0; j < arr[i].length; j++){
if(arr[i][j] > v){
v = arr[i][j];
} else {
Math.max.apply(...arr[i][j]);
}
}
largest.push(v);
}
return largest;
}

console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36`.

You initialize v as 0, since the last array only has negative numbers none of them are gona be higher than 0.

yeah that’s what i thought and i’m still trying to initialize it in a way that would work

You could try
`Number.MIN_VALUE`

if you use Math.max you only need one for loop or if you dont use Math.max you should have two for loop.
when ever you work with array what you have to think first is that how you can acces
the index of the array where the property you want to access, is stored
this is a two dimensional array right?

``````How do you access value stored in a array?
var largest = [1,2,3,4]
How can i access value 1 from largest array ?
console.log(largest[0] )
it is so simple rigtht !
What if it is a two dimensional array
var larget =[[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -18]]

How can I access value 17 ?
console.log(largest[0][0]) now we have to provide two index

is it possible for me to hard code the index of array  every time? No right . so I should use one for loop if it a single dimensional array. if it is two dimensional array, i
have to use two for loop.

How two for loop work together?
for(var i=0;i<2,i++){
for(var j=0;j<3;j++){
console.log(i,j)
so you will get out put as below
value of i      value of j
0                      0
0                      1
0                      2
now j=3
but 3<3 =false second for loop stooped iteration

so next iteration of first for loop start
1                      ..0
1                        1
1                          2

}
now again j is 3 and i is 2
but first for loop iteration stooped because 2<2 is a false statement.
so there is no iteration for second for loop

]

just change
var v=arr[i][0]
there is no need of else statement
your if condition only help you to find biggest number in the array if i am not wrong

Now just try again
do you know how push method work?
var anotherArray=[]
how can i insert one value to this element without using any method like push and unshift

anotherArray[0]=1
anotherArray[1]=5
anatherArray[2]=6

console.log(anotherArray) which is equal to [1,5.6]

just try this

``````

thanks for your explanation, now i get it. by initializing v with a value of 0 it was gonna see that zero in a subarry with neagtive numbers only. this is my updated code and it’s working

``````function largestOfFour(arr) {
// You can do this!
var largest = [];
for(var i = 0; i < arr.length; i++){
var v = arr[i][0];
for(var j = 0; j < arr[i].length; j++){
if(arr[i][j] > v){
v = arr[i][j];
}
}
largest.push(v);
}
return largest;
}

console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));
``````

Another cool way of solving this problem would be using functional approach.

``````function largestOfFour(arr) {
return arr.map(numbers => Math.max(...numbers));
}
``````

You would be able to solve the challenge with a single line of code and would be much easier to read.

1 Like

Math.max is the method which we can use.to find highest number it use for loop to find largest number