Skip to content

Instantly share code, notes, and snippets.

@juanigallo
Created September 8, 2017 18:10
Show Gist options
  • Save juanigallo/c37bb54656c2b812915c843ca4f1279a to your computer and use it in GitHub Desktop.
Save juanigallo/c37bb54656c2b812915c843ca4f1279a to your computer and use it in GitHub Desktop.
Check sum of unordered array in JavaScript
function checkUnorderedSum(arr, sum) {
let alreadySeen = [];
for (var i = 0; i < arr.length; i++) {
let complement = sum - arr[i];
if (alreadySeen.indexOf(arr[i]) > -1) {
return true
}
alreadySeen.push(complement);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment