Skip to content

Instantly share code, notes, and snippets.

@ryankshaw
Created December 1, 2021 00:31
Show Gist options
  • Save ryankshaw/663fd6753e4d7b8b1e44f49f55c052fd to your computer and use it in GitHub Desktop.
Save ryankshaw/663fd6753e4d7b8b1e44f49f55c052fd to your computer and use it in GitHub Desktop.
largestSum
function sum(...nums) {
return nums.reduce((a, b) => a + b, 0)
}
function largestSum(array) {
let largestSumSoFar
for (let i = 0; i < array.length; i++) {
for (let j = i; j < array.length; j++) {
let subArray = array.slice(i, j + 1)
let test = sum(...subArray)
if (typeof largestSumSoFar === 'undefined' || test > largestSumSoFar) {
largestSumSoFar = test
}
}
}
return largestSumSoFar
}
console.log(largestSum([1, 5, -3, 4, -1, 7]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment