函数“子集”告诉我“最大”是未定义的。
到底是怎么回事?
function ArrayAdditionI(arr)
{
arr.sort();
var largest = arr.pop()
console.log(largest);
subset([], arr, largest);
}
function subset(soFar, rest, largest)
{
var sum = 0;
if (rest.length === 0)
{
for(var i=0; i<soFar.length; i++)
{
sum+= soFar[i];
}
console.log("sum = "+ sum + " " + largest);
if (sum === largest) return true;
}
else
{
var soFar2 = soFar.slice(0);
soFar2.push(rest[0]);
subset(soFar,rest.slice(1));
subset(soFar2, rest.slice(1));
}
}
ArrayAdditionI([85,3,88,2])