Skip to content

Instantly share code, notes, and snippets.

@fbohz
Last active August 20, 2020 14:49
Show Gist options
  • Save fbohz/0ccfbd2cadb80dfdb71afe8689c8ac33 to your computer and use it in GitHub Desktop.
Save fbohz/0ccfbd2cadb80dfdb71afe8689c8ac33 to your computer and use it in GitHub Desktop.
largestSubarraySum(array)
function largestSubarraySum(array) {
let currentSum = 0
let largestSum = 0
function getCurrentSum(currentSum, currentNumber) {
let sum = currentNumber + currentSum
if (sum > 0) {
currentSum = sum
}
if (sum <= 0) {
currentSum = 0
}
return currentSum
}
for (let n of array) {
// console.log('BEF', currentSum)
currentSum = getCurrentSum(currentSum, n)
// console.log('AFTER', currentSum)
if (currentSum > largestSum) {
largestSum = currentSum
}
}
return largestSum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment