# One test case failing - Return Largest Numbers in Arrays

My nested for-loop doesn’t seem to work with arrays consisting of negative values.

Objective: Return an array consisting of the largest number from each provided sub-array

function largestOfFour(arr) {
let largestNumber = 0;
let tocopy=[];
for(let i=0; i<arr.length; i++){
for(let j=0; j<arr[i].length; j++){
if(arr[i][j]>largestNumber){
largestNumber = arr[i][j];
}
}
console.log(arr[i], largestNumber)
tocopy.push(largestNumber)
largestNumber = 0;
}
}

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

PASSED Test Cases:

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])
largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]])
//>>> returns [27, 5, 39, 1001].
largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) //>>> returns [9, 35, 97, 1000000].

FAILED Test Case:

largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]])
//>>> should return [25, 48, 21, -3].
//>>> current return value: [ 25, 48, 21, 0 ]

I see…That makes sense.
I am assuming I have to make changes within my second inner for-loop. When I set
j>arr[i].length; instead of j<arr[i].length;, I am able to identify the smallest number.

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