# Generate a Countdown of Numbers in an Array
Create a function that takes a number as an argument and returns an array of numbers counting down from this number to zero.
# Examples
countdown(5) ➞ [5, 4, 3, 2, 1, 0]
countdown(1) ➞ [1, 0]
countdown(0) ➞ [0]
1
2
3
4
5
2
3
4
5
# Notes
The argument will always be greater than or equal to zero.
# 答案(仅供参考)
# 解法 1
function countdown(start) {
const arr = [];
for (let index = start; index >= 0; index--) {
arr.push(index);
}
return arr;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 解法 2
const countdown = (start) => [...Array(start + 1).keys()].reverse();
1
# 解法 3
function countdown(start) {
return [...Array(start + 1)].map((_, i) => start - i);
}
1
2
3
2
3