Hi, so I’ve been doing this challenge and I actually passed it, but I don’t really like the way I did it.
Originally I reset the variable largestNumber to 0, but it didn’t pass the final test because 0 > -3 and it would put 0 in the array. So I reset largestNumber to -10, so it works now, but I don’t really like this method because if you were to input a number smaller than -10 it wouldn’t work.
How could I fix this while maintaining the same code structure?
function largestOfFour(arr) {
let a = [];
let largestNumber = 0;
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];
}
}
a.push(largestNumber);
largestNumber = -10;
}
console.log(a);
return a;
}
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/92.0.4515.159 Safari/537.36
Challenge: Return Largest Numbers in Arrays
Link to the challenge: