【发布时间】:2021-03-11 05:59:23
【问题描述】:
// Only change code below this line
var myArray = [];
function countdown(n){
if (n < 1) {
console.log('Finished array: ' + myArray);
return myArray;
} else {
console.log('Pushing Value!');
myArray.push(n)
console.log('Calling function countdown');
countdown(n - 1)
}
}
console.log(countdown(10));
// Only change code above this line
代码感觉应该是正确的,但是函数返回undefined。
我认为这与递归有关......
(规则:)
countdown(-1) should return an empty array.
countdown(10) should return [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
countdown(5) should return [5, 4, 3, 2, 1]
Passed
Your code should not rely on any kind of loops (for, while or higher order functions such as forEach, map, filter, and reduce).
Passed
You should use recursion to solve this problem.
【问题讨论】:
-
您的
else案例没有返回声明。你的意思是return countdown(n - 1)?
标签: javascript arrays recursion return