我正在学习一些JavaScript,但我很难理解关于递归倒计时(递归倒计时,链接)的FreeCodeCamp课程。
在这节课中,有一个最初的例子。但我很困惑它是如何运作的:
function countup(n) {
if (n < 1) {
return [];
} else {
const countArray = countup(n - 1);
countArray.push(n);
return countArray;
}
}
console.log(countup(5));
我看到这个函数执行的步骤(按顺序),如下所示:
n (开头为5)大于1,因此转到else语句。else语句中,我将分配一个常量(countArray)来执行countup(n - 1),即4。countArray.push(n),因此它将把4推入数组中。return countArray,即4。n==0,在这种情况下,函数结束返回一个空数组。我知道这不是它的工作方式,我只是简单地“描述”我的noob思维是如何看待这个递归函数的。
我看了这个练习的解决方案和其他人的解释,但是我不明白为什么这个函数不像我所描述的那样工作,也不明白为什么在到达n==0之后,它开始填充一个从1到5的数组。
我想了解这一点。
发布于 2021-06-01 17:28:13
认识到countup的每一次执行都会有自己的n和countArray变量。
它可能有助于想象它。每个执行上下文都形象化为一个“框”。当函数调用返回时,外部框中的变量仍然存在。
最外面的框是由初始调用:countup(5)创建的执行上下文。
// n is 5 and does not change
const countArray = countup(n - 1);
+-------------------------------------------------------------------------------+
| // n is 4 and does not change |
| const countArray = countup(n - 1); |
| +-------------------------------------------------------------------------+ |
| | // n is 3 and does not change | |
| | const countArray = countup(n - 1); | |
| | +-------------------------------------------------------------------+ | |
| | | // n is 2 and does not change | | |
| | | const countArray = countup(n - 1); | | |
| | | +-------------------------------------------------------------+ | | |
| | | | // n is 1 and does not change | | | |
| | | | const countArray = countup(n - 1); | | | |
| | | | +-------------------------------------------------------+ | | | |
| | | | | // n is 0 and does not change | | | | |
| | | | | return []; // the if-block is executed because n < 1 | | | | |
| | | | +-------------------------------------------------------+ | | | |
| | | | // countArray is [] | | | |
| | | | countArray.push(n); // n is still 1 | | | |
| | | | return countArray; // returns [1] | | | |
| | | +-------------------------------------------------------------+ | | |
| | | // countArray is [1] | | |
| | | countArray.push(n); // n is still 2 | | |
| | | return countArray; // returns [1, 2] | | |
| | +-------------------------------------------------------------------+ | |
| | // countArray is [1, 2] | |
| | countArray.push(n); // n is still 3 | |
| | return countArray; // returns [1, 2, 3] | |
| +-------------------------------------------------------------------------+ |
| // countArray is [1, 2, 3] |
| countArray.push(n); // n is still 4 |
| return countArray; // returns [1, 2, 3, 4] |
+-------------------------------------------------------------------------------+
// countArray is [1, 2, 3, 4]
countArray.push(n); // n is still 5
return countArray; // returns [1, 2, 3, 4, 5]发布于 2021-06-01 17:27:53
您可以添加一些日志来可视化正在发生的事情:
function countup(n) {
if (n < 1) {
console.log('n = %d, returning empty array', n);
return [];
} else {
console.log('n = %d, calling countup(%d - 1)', n, n);
const countArray = countup(n - 1);
console.log('n = %d, countArray is %s', n, JSON.stringify(countArray))
console.log('n = %d, pushing n onto array', n);
countArray.push(n);
console.log('n = %d, returning %s', n, JSON.stringify(countArray));
return countArray;
}
}
console.log(countup(5));
发布于 2021-06-01 17:26:45
下面是每个函数调用中数组的样子,如果这有帮助的话:
Array: 1 N: 1
Array: 1,2 N: 2
Array: 1,2,3 N: 3
Array: 1,2,3,4 N: 4
Array: 1,2,3,4,5 N: 5https://stackoverflow.com/questions/67793271
复制相似问题