Skip to content

Instantly share code, notes, and snippets.

@Xaz16
Last active June 2, 2017 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xaz16/289ed1a9c76510c56ef137133e51aefe to your computer and use it in GitHub Desktop.
Save Xaz16/289ed1a9c76510c56ef137133e51aefe to your computer and use it in GitHub Desktop.
Return biggest sum of element in subarray
function largestOfFour(arr) {
var summs = [];
var index = 0;
for(var i = 0; i < arr.length; i++) {
summs.push(arr[i].reduce(function(prev, current, index) {
return prev + current;
}));
}
for(var j = 0; j < summs.length; j++) {
index = j !== 0 ? summs[j] > summs[j-1] ? j : summs[0] : summs[j];
}
return arr[index];
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment