Javascript &引用;返回arr";在递归函数的if语句中不起作用。为什么?
此函数用于确定该场可划分的最大等边部分Javascript &引用;返回arr";在递归函数的if语句中不起作用。为什么?,javascript,Javascript,此函数用于确定该场可划分的最大等边部分 function field (width, height) { let big = Math.max(width, height); let small = Math.min(width, height); if (big / small === 1){ let arr = []; arr.push(big); arr.push(small); return arr; } field(big-sma
function field (width, height) {
let big = Math.max(width, height);
let small = Math.min(width, height);
if (big / small === 1){
let arr = [];
arr.push(big);
arr.push(small);
return arr;
}
field(big-small, small);
}
field(1600, 500) // should be [100, 100]
它返回
undefined
,但如果我以前使用console.log
,它会显示一切正常。为什么?递归调用需要返回语句
return field(big - small, small);
功能字段(宽度、高度){
设big=Math.max(宽度、高度);
设small=Math.min(宽度、高度);
如果(大===小){//更容易检查
设arr=[];
arr.push(大);
arr.push(小);
返回arr;
}
返回字段(大-小,小);
}
控制台日志(字段(1600500))代码>如果<代码>大/小!=1
函数不返回任何内容(未定义)。欢迎使用堆栈溢出。您正在变得未定义,因为您不仅在big/small===1时返回,而且在您的情况下不会返回。只需更改此行字段(大-小,小)代码>带返回字段(大-小,小)
然后该函数将按预期方式开始工作。