Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Countdown Recursively

A Closer Look at Functionsmedium

Course · Section 10: A Closer Look at Functions · Lecture 137: First-Class and Higher-Order Functions

Implement `countDown(n)` returning `[n, n-1, ..., 1]` using recursion, with `countDown(0)` returning an empty array.

Sample tests

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

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Appending instead of prepending reverses the order.

Learning resources

  • MDN: Recursion
Approach & explanation (try first)

Prepending n to the countdown of n-1 produces the descending array, ending at [].

Loading...
⌘/Ctrl + Enter

Run your code to see results.