英文:
Probleme in recursion JavaScript
问题
Sure, here's the translation of the code-related part:
"我是JavaScript的初学者,想知道为什么要将countdown(n - 1)
赋值给countArray
,而不使用push()
,请解释。"
Please note that I won't provide translations for the actual JavaScript code. If you have any other questions or need further clarification, feel free to ask.
英文:
I'm a beginner in JavaScript and would like to know what is the point of assign countdown(n - 1)
to countArray
and why we don't use push()
instead of unshift () explanation
, please
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function countdown(n){
if (n < 1) {
return [];
} else {
var countArray=countdown(n - 1);
countArray.unshift(n);
return countArray;
}
}
console.log(countdown(5)); // [5, 4, 3, 2, 1]
<!-- end snippet -->
答案1
得分: 0
所以在这里你给出一个数字,比如5,它将创建一个反向顺序的数组,如[5,4,3,2,1]。
Unshift基本上是将元素放在数组的第一个位置。
让我们逐步进行说明。
首先它将检查5 < 1,不成立,因此它进入else条件并创建一个带有countdown(4)的函数调用。
因此,类似地,它将创建函数堆栈,直到倒计时(1),因为当它达到0时,它将返回一个空数组,因为if条件匹配。
然后它将从堆栈距离顶部解析,
堆栈跟踪将是
- countdown(1)
- countdown(2)
- countdown(3)
- countdown(4)
- countdown(5)
所以空数组已经返回到那个空数组,countdown(1)将执行并在第一位置执行unshift
现在数组将是[1]
接下来它将执行countdown(2)并unshift到第一个索引,所以1移到了第二位置
现在数组将是[2,1]
同样
它达到了countdown(5)
并且数组将是[5,4,3,2,1]
就是这样
英文:
So here you give a number like 5, so it will create an array with reverse order like [5,4,3,2,1].
Unshift is basically to put the element at the first of the array
Let us go by one by one step
first it will check 5<1, no so it goes to else condition and creates a function call with countdown(4),
so like wise it will create function stack till 1 countdown(1) because when it reaches 0 it will return an empty array since if condition matches
so then it will resolve from top of the stacktrace,
stacktrace will be
- countdown(1)
- countdown(2)
- countdown(3)
- countdown(4)
- countdown(5)
so the empty array has been returned to that empty array countdown(1) will execute and unshift to the first position
now array will be [1]
next it will execute the countdown(2) and unshift means to the first index it will add so the 1 gets moved to the second position
now array will be [2,1]
like wise
it reaches countdown(5)
and array will be [5,4,3,2,1]
thats it
答案2
得分: 0
countdown(n - 1)
调用了将 n
减一后的值作为参数的 countdown
函数。例如,如果你的第一个调用是 countdown(5)
,当遇到 countdown(n - 1)
这行代码时,countdown
被调用并传入值 4 - (countdown(4)
).
这个过程会一直持续,直到 n
的值小于 0,此时函数会返回一个空数组。
unshift
从数组中移除一个值,而 push
则将一个值添加到数组中。这段代码逐渐清空数组,因此使用了 unshift
。
英文:
countdown(n - 1)
calls the countdown function with the value of n
decremented by one. So for example, your if first call is countdown(5)
, when the line countdown(n - 1)
is encountered, countdown is called with the value 4 - (countdown(4)).
This continues until the value of n is less than 0 which is when an empty array is returned from the function.
unshift
removes a value from the array while push
adds onto an array. This code gradually empties the array and that is why unshift is used.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论