To find the largest sum in a JavaScript array of arrays, you can loop through each sub-array, sum its elements, and keep track of the largest sum so far:
var arrays = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
var largestSum = Number.NEGATIVE_INFINITY;
for (var i = 0; i < arrays.length; i++) {
var currentArray = arrays[i];
var currentSum = 0;
for (var j = 0; j < currentArray.length; j++) {
currentSum += currentArray[j];
}
if (currentSum > largestSum) {
largestSum = currentSum;
}
}
console.log(largestSum); // Output: 45
In this example, largestSum
is a variable that keeps track of the largest sum found so far. The outer loop iterates through each sub-array in the arrays
array, and the inner loop sums the elements of the current sub-array. The if
statement checks whether the current sum is greater than the largest sum found so far, and updates largestSum
if it is.
+ There are no comments
Add yours