Return Largest Numbers in Arrays
Guys, my code works only with some test cases. I have problems with the last one. Can anyone explain to me why I get [ 25, 48, 21, -10 ] instead of [25, 48, 21, -3] in the last example?
const largestOfFour = (arr) => {
let new_arr = [];
for (let i = 0; i < arr.length; i++) {
let max = arr[i][0];
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j] > max && arr[i][j] > 0 || arr[i][j] < 0) {
max = arr[i][j];
}
}
new_arr.push(max);
}
return new_arr;
};
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])); //[5, 27, 39, 1001]
console.log(largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]])); //[27, 5, 39, 1001]
console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]])); //[25, 48, 21, -3]
Challenge: Return Largest Numbers in Arrays
Link to the challenge: