Tell us what’s happening:
Dont know how to deal with the array fill with negative numbers. My code works for all the other arrays:
largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]) should return [25, 48, 21, -3].
How do i find the biggest out of negative numbers since my max was set to 0 hence it would not be able to work for the negative array?
Your code so far
function largestOfFour(arr) {
// You can do this!
var newA = [];
var max;
for(let i = 0; i < arr.length; i++){
max = arr[i].length;
for(let j = 0; j < arr[i].length; j++){
if(arr[i][j] > max){
max = arr[i][j];
}
}
newA.push(max);
}
console.log(newA);
return newA;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays