Return Largest Numbers in Arrays. What is wrong with code?

Return Largest Numbers in Arrays. What is wrong with code?
0.0 0

#1

Tell us what’s happening:

Your code so far


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

}


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/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays


#2

your code looks for a number bigger than 0 when it finds it, (eg. it finds the first ‘4’ in the first sub-array), it takes the whole sub array of [4,5,1,3] and assigns it to longest.

This doesn’t fulfill the requirement of the challenge.
The challenge wants you to search each array for the biggest number. For eg. [4,5,1,3] has the biggest number of 5
once you find each big number in each array, create a new array of all the big numbers…


#3

Adding to what @hbar1st already mentioned, make sure you do not use the return statement until you finish looking at each nested array inside arr. You currently only look at the first element of arr before returning something.