Hello. I wondered how JavaScript knows to append results to the variable results
with the statement results[n] = largestNumber;
in the basic code solution provided on freeCodeCamp Algorithm Challenge Guide: Return largest Numbers in Arrays
I would think we would need to use results.push(largestNumber)
instead.
function largestOfFour(arr) {
var results = [];
for (var n = 0; n < arr.length; n++) {
var largestNumber = arr[n][0];
for (var sb = 1; sb < arr[n].length; sb++) {
if (arr[n][sb] > largestNumber) {
largestNumber = arr[n][sb];
}
}
results[n] = largestNumber;
}
return results;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);