Created
October 26, 2014 22:22
-
-
Save crullian/303455bc8bd33d65f188 to your computer and use it in GitHub Desktop.
ArrayAdditionI
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 ArrayAdditionI(arr) { | |
arr.sort(function(a, b) { | |
return a - b; | |
}); | |
var largest = arr[arr.length - 1]; // Set largest to last (largest) array value | |
for (var i = 0; i < arr.length; i++) { // Start outer loop | |
var sum = arr[i]; | |
for (var j = 0; j < arr.length; j++) { // Start inner to begin sum | |
if (i != j) { // Ensure we don't add the same array index to itself | |
sum += arr[j]; | |
console.log(sum); | |
if (sum === largest) { | |
return true; | |
} | |
} | |
} | |
for (j = 0; j < arr.length; j++) { // If no match, start 2nd loop to re-iterate removing index values | |
if (i != j) { | |
sum -= arr[j]; | |
console.log(sum); | |
if (sum === largest) { | |
return true; | |
} | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment