Created
December 1, 2021 00:31
-
-
Save ryankshaw/663fd6753e4d7b8b1e44f49f55c052fd to your computer and use it in GitHub Desktop.
largestSum
This file contains hidden or 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 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