Chapter 3 · Data

Recursion

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 20 · Concept

A function can call itself

By the end of this lesson you write functions that call themselves, and read the error when they never stop.

function countdown(n) {
  if (n === 0) {
    console.log("Go!");
    return;
  }
  console.log(n);
  countdown(n - 1);
}

countdown calls itself with a smaller n. That is recursion. The base case, n === 0, does not call again, so the calls stop.

function countdown(n) {
  if (n === 0) {
    console.log("Go!");
    return;
  }
  console.log(n);
  countdown(n - 1);
}
countdown(2);
function sumDigits(n) {
  // 1. below 10: the sum is n itself
  // 2. else: last digit + sumDigits of the rest
}
console.log(sumDigits(1234));
console.log(sumDigits(7));
function down(n) {
  return down(n + 1);
}
console.log("Start");
down(0);
function sumTo(n) {
  return n + sumTo(n - 1);
}
console.log(sumTo(5));
let calls = 0;
function fib(n) {
  calls++;
  if (n <= 1) {
    return n;
  }
  return fib(n - 1) + fib(n - 2);
}
function fibLoop(n) {
  let a = 0;
  let b = 1;
  for (let i = 0; i < n; i++) {
    [a, b] = [b, a + b];
  }
  return a;
}
console.log(fib(10), fibLoop(10));
console.log("calls:", calls);
function sumAll(xs) {
  let total = 0;
  for (const x of xs) {
    if (Array.isArray(x)) {
      total += sumAll(x);
    } else {
      total += x;
    }
  }
  return total;
}
console.log(sumAll([1, [2, 3], [4, [5]]]));
function flatten(xs) {
  const flat = [];
  // 1. a number: push it
  // 2. an array: flatten it, push its items
  return flat;
}
console.log(flatten([1, [2, 3], [4, [5]]]).join(", "));
// isPalindrome(word): true when the word reads
// the same both ways
console.log("level", isPalindrome("level"));
console.log("lever", isPalindrome("lever"));
function fib(n) {
  if (n <= 1) {
    return n;
  }
  return fib(n - 1) + fib(n - 2);
}
console.log(fib(50));
Terminal
Step 1 of 9A countdown that calls itself

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)