有人可以直观地解释这里发生了什么。另一个递归JavaScript函数
var stack = [];
function countDown(int) {
stack.push(int);
if (int === 1) {
return 1;
}
return countDown(int - 1);
}
function multiplyEach() {
// Remove the last value of the stack
// and assign it to the variable int
int = stack.pop();
x = stack.length;
// Base case
if (x === 0) {
return int;
}
// Recursive case
else {
stack[x - 1] = int * stack[x - 1];
return multiplyEach();
}
}
//调用函数
countDown(7);
//然后打印出来()
console.log(multiplyEach());
我有几分明白,就是它的堆叠建立并乘以multiplyEach返回的值一切都在一起,但我无法想象它。
然后:
Stack[x-1] is getting me
这个问题看起来不完整。你做完了吗? – kojiro 2012-04-03 12:40:16
良好的格式是开始理解代码的好方法 – 2012-04-03 12:41:00
我在试图了解的手机上。格式不完美。这就是我的全部答案。问题已完成 – Sam 2012-04-03 12:48:48