Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Countdown

A Closer Look at Functionsmedium

Course · Section 10: A Closer Look at Functions · Lecture 135: Default Parameters

Implement `countdown(n)` returning the array `[n, n-1, ..., 1]`, or an empty array when `n` is `0`.

Sample tests

Input: countdown(3)
Output: [3,2,1]
Input: countdown(1)
Output: [1]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • An off-by-one bound that includes 0 or skips n.

Learning resources

  • MDN: Functions
Visualize this concept: The Scope Chain →
Approach & explanation (try first)

A loop from n down to 1 collects the descending sequence, returning [] when n is 0.

Loading...
⌘/Ctrl + Enter

Run your code to see results.