Recursion

About

Resolve something by breaking down the problem and re-looking at it until you reach a base case.

function factorial(n: number): number {
    // Base case
    if (n === 1) return 1;
    
    // Recurse
    return n * factorial(n - 1);
}

3 sections of recursion

Recursive functions can be broken down in three sections:

function factorial(n: number): number {
    /**
     * Pre-recurse, things to do before recursing
     */
     
     /**
      * Recurse
      */
     factorial(n - 1);
     
    /**
     * Post-recurse, things to do after recursing
     */
}

Last updated