November 27th, 2024
What's missing in this code?
indexOfMaxSum([[1, 2, 3], [4, 5], [6, 7, 8]]); // 2
indexOfMaxSum([[1, 1, 1], [2, 2], [3]]); // 1
indexOfMaxSum([[1, 5, 1], [2, 2], [3, 1]]); // 0
indexOfMaxSum([[1, 5, 1], [2, 2], [3, 4, 1]]); // 2
function indexOfMaxSum(arr) {
let maxSum =
;
let maxIndex =
;
arr.forEach((subArray, index) => {
const sum = subArray.reduce((acc, val) => acc + val, 0);
if (sum
maxSum)
maxSum = sum;
maxIndex = index;
}
});
return maxIndex;
}
Type or select from these options