Base Case First
You just saw that the base case is what lets a recursion finish. It's so important that we have a rule: write the base case first, before you write the part that calls itself.
Every recursive function has exactly two pieces:
- Base case — the simplest input, where you know the answer outright and
return without recursing. (
countdown(0)→[].) - Recursive step — solve a slightly smaller problem and combine.
(
countdown(n)→[n, ...countdown(n - 1)].)
Why first? Because the recursive step has to move toward the base case. If you forget the base case, the function never stops:
function broken(n) {
return n + broken(n - 1); // ❌ no base case → runs until it crashes
}
That crash even has a name: a stack overflow — the call stack grew until it ran out of room. The fix is always the same: add a base case the recursion actually reaches.
Read carefully, then answer below. 👇