Created
June 20, 2014 19:21
-
-
Save irrationalistic/3a369cb70b59f7a86a78 to your computer and use it in GitHub Desktop.
Coderbyte - Array Addition
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 ArrayAddition(arr) { | |
// calculate the largest possible number | |
var startArr = arr.sort(function(a,b){return b-a}); | |
var largest = startArr.shift(); | |
// return true if the current total plus any number is | |
// the largest option | |
var checker = function(curArr, curTotal){ | |
// Go through each item in the current set | |
for(var i = 0; i < curArr.length; i++){ | |
var newTotal = curTotal + curArr[i]; | |
// if this item plus the current running total adds | |
// up to the largest number, this is considered successful | |
if(newTotal === largest){ | |
return true; | |
} else { | |
// if they don't match yet, let's try the next level down | |
// clone the array... | |
var temp = curArr.slice(); | |
// remove the current item in the main loop | |
temp.splice(i, 1); | |
// run checker again. If it comes back as true, | |
// quit out of this instance too | |
if(checker(temp, newTotal)){ | |
return true; | |
} | |
} | |
} | |
// if we get this far, just give back false | |
return false; | |
} | |
return checker(startArr, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment