Last active
August 20, 2020 14:49
-
-
Save fbohz/0ccfbd2cadb80dfdb71afe8689c8ac33 to your computer and use it in GitHub Desktop.
largestSubarraySum(array)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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