我正在尝试创建一个从1开始的递归程序,并找到一个可能的序列,可以添加5或乘以3来达到给定的整数参数,并且我不断收到错误“TypeError:undefined is a a a功能“。
感谢您的帮助!
var ii; var current_array; var target;
function puzzle(target, current_array) {
if (current_array==undefined)
current_array=[];
var current_array=current_array;
var current_total=1;
for (ii=0;ii<current_array.length;ii++) {
if (current_array[ii]==3)
current_total*=3;
else current_total+=5;
}
if (current_total>target)
return null;
else if (current_total==target){
return current_array;
}
else {return puzzle(target, current_array.push(3))||
puzzle(target, current_array.push(5));
}
}
答案 0 :(得分:0)
根据您的代码示例很难说,但我会尝试使用typeof来测试未定义的变量:
if (typeof(current_array) === 'undefined')
current_array = [];