Created
March 17, 2018 02:34
-
-
Save npgenx/60b9e20b33f1105d93bf7fd9bd8d6d50 to your computer and use it in GitHub Desktop.
js: Sum Test
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
const isThereAMaxMatch = (list) => { | |
let thereIsASum = false; | |
let sortedList = list.sort((a, b) => { return b - a }); // hi to low | |
const maxNumber = sortedList[0]; | |
let numCheck = (list) => { | |
let accumulatedSum = list[0]; | |
for (i = 1; i < list.length; i++) { | |
if (accumulatedSum < maxNumber) { | |
let tempSum = accumulatedSum + list[i]; | |
if (tempSum == maxNumber) { | |
thereIsASum = true; | |
console.log(`We have a match`); | |
return thereIsASum; // spits out that we have a match and returns true | |
} | |
if (tempSum < maxNumber) { accumulatedSum = tempSum } //add the increment | |
} | |
} | |
}; | |
for (i = 0; i < sortedList.length; i++) { | |
sortedList.shift(); | |
let test = numCheck(sortedList); | |
if (test) { break; } | |
} | |
if (!thereIsASum) { | |
console.log(' we did not find a sum ') | |
} | |
}; | |
//a test | |
isThereAMaxMatch([1, 2, 4, 5, 7, 8, 20]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment