I get the error "Maximum call stack size exceeded error" when calling this recursive JavaScript.
This is my code:
var num = 12
,maxlength = 50;
var lengths = [5,6,7,8,4,4,5,6,3,3,2,2];
function knap(maxlength,num){
if(maxlength==0) return 1;
if(maxlength<0||(maxlength>0&&num<1)){
return 0;
}
if(knap(maxlength-lengths[num],num-1)){
console.log(lengths[num]+" ")
return 1;
}
return knap(maxlength,num-1);
}
if(knap(maxlength,num)){
console.log('Yes');
}else{
console.log('No');
}
How can I figure out this?