我想使用递归来通过指定的步骤找到给定整数的范围。
given number --> -20
step --> 4
returns --> [ -20, -16, -12, -8, -4, -2, 0, 2, 4 ]
到目前为止,我已经能够创建一个递归函数来返回相应的结果:
function range(num,step,res=[]) {
const s = num < step ? step : -step;
if (num === step + s) return res;
return num === step ? [...res,num] : range(num+s,step,[...res,num]);
}
console.log(range(5,1)); // [ 5, 4, 3, 2, 1 ]
console.log(range(-8,2)); // [ -8, -6, -4, -2, 0, 2 ]
console.log(range(-20,4)); // [ -20, -16, -12, -8, -4, -2, 0, 2, 4 ]
但是,以下调用返回stackoverflow
console.log(range(-7,2)); // stackoverflow!
console.log(range(11,5)); // stackoverflow!
我知道代码有问题,但我就是搞不清楚是什么。有人能帮我指明正确的方向吗?或者告诉我我做错了什么。百万感谢:(
更新:
由于TazHinkle的控制流程,我能够解决以下问题:
function range(num,step,res=[]) {
const s = num < step ? step : -step;
if (num > step && num < step) return res;
if (num < step && num + s > step) return [...res,num];
return num === step ? [...res,num] : range(num+s,step,[...res,num]);
}
console.log(range(5,1)); // [ 5, 4, 3, 2, 1 ]
console.log(range(-8,2)); // [ -8, -6, -4, -2, 0, 2 ]
console.log(range(-20,4)); // [ -20, -16, -12, -8, -4, 0, 4 ]
console.log(range(11,5)); // [ 11, 6, 1 ]
console.log(range(-7,2)); // [ -7, -3, -5, -1, 1 ]
console.log(range(-9,4)); // [ -9, -5, -1, 3 ]
大小写范围(-7,2(在当前版本中无法解析,因为它将在1和3之间无限步进(因为它正在步进2并试图达到2,但永远不会(。当它经过时,你可以用这样的东西让它放弃:
function range(num,step,res=[]) {
const s = num < step ? step : -step;
const forward = num < step ? true : false;
if (num === step + s) return res;
if(forward) {
if(num + s > step) {
return [...res,num]
}
return num === step ? [...res,num] : range(num+s,step,[...res,num]);
}else {
if(num + s < step) {
return [...res,num]
}
return num === step ? [...res,num] : range(num+s,step,[...res,num]);
}
}
这是在假设您的目标是从第一个参数开始,然后逐步下降,直到最后一个数字是0、1或-1(基本上尽可能接近零(的情况下进行的。
首先,不要将step
从neg/pos翻转,而是将neg转换为pos,并为neg步骤设置一个标志:
if (num < 0) {
n = Math.abs(num);
neg = true;
} else n = num;
删除这个额外的步骤,它没有给奇数参数留下停止的方法:
if (num === step + s) return res;
这一行中唯一的区别是<
而不是===
。这将使奇数接近0,偶数接近零:
let result = n < step ? [n, ...res] : rangeToZero(n -step, step, [n, ...res]);
最后,如果neg == true
,我们将每个数字设为负数(感觉像作弊(:
if (neg) {
return result.map(N => -Math.abs(N));
}
function rangeToZero(num, step, res=[]) {
let n, neg = false;
if (num < 0) {
n = Math.abs(num);
neg = true;
} else n = num;
let result = n < step ? [n, ...res] : rangeToZero(n -step, step, [n, ...res]);
if (neg) {
return result.map(N => -Math.abs(N));
}
return result;
}
console.log(rangeToZero(5,1)); // [ 5, 4, 3, 2, 1, 0 ]
console.log(rangeToZero(-8,2)); // [ -8, -6, -4, -2, 0 ]
console.log(rangeToZero(-20,4)); // [ -20, -16, -12, -8, -4, 0 ]
console.log(rangeToZero(10, 3)); // [ 10, 7, 4, 1 ]
console.log(rangeToZero(-7,2)); // [ -7, -3, -5, -1 ]
console.log(rangeToZero(11,5)); // [ 11, 6, 1 ]