curriculum/challenges/english/blocks/lab-countdown/5cd9a70215d3c4e65518328f.md
In this lab, you will build a countdown function that takes a starting number and generates a countdown list.
When you call countdown(n), it should return an array that starts at n and counts down by 1 until it reaches 1.
For example:
countdown(5) should return [5, 4, 3, 2, 1]countdown(1) should return [1]If the starting number is less than 1, there is nothing to countdown, so return an empty array:
countdown(0) should return []countdown(-3) should return []To complete the lab, you must build the result using recursion and you must not use loops (for, while), Array.from(), or array-iteration methods (forEach, map, filter, reduce). Each call should produce its own result array (don't use globals to store the countdown).
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories:
countdown.countdown function should take a single argument n (a number).n is less than 1, countdown should return an empty array.countdown should return an array containing the integers from n down to 1 in descending order.for, while, Array.from(), or higher-order methods like forEach, map, filter, reduce).n - 1) so the function reaches its base case.countdown multiple times with different inputs should always return the correct, independent result.You should have a function named countdown.
assert.isFunction(countdown);
countdown(-1) should return an empty array.
assert.isEmpty(countdown(-1));
countdown(10) should return [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
assert.deepStrictEqual(countdown(10), [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
countdown(5) should return [5, 4, 3, 2, 1]
assert.deepStrictEqual(countdown(5), [5, 4, 3, 2, 1]);
Your code should not rely on any kind of loops (for, while, Array.from() or higher order functions such as forEach, map, filter, and reduce).
assert(
!__helpers.removeJSComments(code).match(/for|while|forEach|map|filter|reduce|Array\.from/g)
);
You should use recursion to solve this problem.
assert.isAtLeast(
__helpers.removeJSComments(countdown.toString()).match(/countdown\s*\(.+\)/g)?.length,
2
);
Global variables should not be used to cache the array.
countdown(1)
assert.deepStrictEqual(countdown(5), [5, 4, 3, 2, 1]);
function countdown(n) {
if (n < 1) {
return [];
}
return [n, ...countdown(n - 1)];
}