I have successfully made a function which takes an array of arrays and returns a new array with each of the largest numbers from the nested arrays.
However, to achieve this I have had to set my ‘currHigh’ variable to negative 1 million to avoid issues with the occurrence negative numbers within the arrays.
Please could someone offer up a more air-tight method that would not falter should -1000001 or larger appear in any of the arrays?
Kindest regards
function largestOfFour(arr) {
const newArr = [];
for (let each of arr) {
let currHigh = -1000000;
for (let i = 0; i < each.length; i++) {
if (each[i] > currHigh) {
currHigh = each[i];
}
}
newArr.push(currHigh);
}
return newArr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Not sure how you got that meaning from my answer. I mentioned that you can use -Infinity but that doing so would be lazy. And then I said I suggest you find yet another way. But again, you seem intent on just writing solutions, so pls go ahead and use the -Infinity method if it pleases you.
You can set the value of currHigh with the first element of the nested array and iterate though the nested array to compare it with rest of the elements