Typorithm · easy

Recursion Basics

recursion-basicspatternsbase-casecall-stack
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Recursion Basics

0%

Basic Patterns

Recursion solves a problem by combining the answer to a smaller version of itself.

Every recursive function needs a base case that returns without recursing.

The recursive step must shrink the input toward that base case.

Each call waits on the stack until the smaller call returns its answer.

01

Base case first, always.

02

Shrink the input every call.

03

Trust the smaller call's answer.

function factorial(n) {
  if (n <= 1) {
    return 1;
  }

  return n * factorial(n - 1);
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Base case

The smallest input answered directly, with no further calls.

if (n <= 1)

2. Base value

factorial(1) and factorial(0) are both 1 by definition.

return 1

3. Recursive step

Combine the current value with the answer for a smaller input.

return n * factorial(n - 1)

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Recursion Basics: Lesson & Practice · Typorithm | ApplyDjinn