The goal is to find if any combination of integers in the array equal the largest integer in the array.
function ArrayAdditionI(arr) {
arr.sort(function(a,b){
return a - b;
});
var largest = arr.pop();
function recursion(target,array){
if(array.length === 0){
return target === 0;
}
var n = array[0];
array = array.slice(1);
return recursion(target,array) || recursion(target - n, array);
}
return recursion(largest,arr);
}
This solution seems to works but I cannot follow it. At the bottom of the recursion function when it reaches the right side of the OR operator I would think it would almost always return false, however it continues recursing. Can someone explain?