My problem is the same as in this question , but the only difference is that I have to return the number of ways in which the steps can be climbed according to a given array of permitted steps. For example, if steps to be climbed are 4
and the permitted steps are [1,3]
, the result should be 3
. Combinations:[(1,1,1,1),(1,3),(3,1)]
I tried to modify the code as :
function add(x,arr){
let result = 0
if(x<0)
return 0;
if(x===0)
return 1;
else{for(let i=0 ; i<arr.length ; i++){
result += add(x-arr[i]);
}
return result};
}
add(4,[1,3])
but this returns an error saying cannot read property length of "undefined"
.How do I correct this?