Skip to content

Instantly share code, notes, and snippets.

@npgenx
Created March 17, 2018 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npgenx/60b9e20b33f1105d93bf7fd9bd8d6d50 to your computer and use it in GitHub Desktop.
Save npgenx/60b9e20b33f1105d93bf7fd9bd8d6d50 to your computer and use it in GitHub Desktop.
js: Sum Test
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