Chapter 9, Designing Functions – Recursion

9.1. Into reverse: An empty string is reversed by simply doing nothing. To reverse a non-empty string, remove its first character, reverse the rest, and append the removed character at the end. For example, reverse("MONTEVIDEO") can be found by using reverse("ONTEVIDEO")+"M". In the same way, reverse("ONTEVIDEO") would be equal to reverse("NTEVIDEO")+"O", and so on:

const reverse = str =>  str.length === 0 ? "" : reverse(str.slice(1)) + str[0];

9.2. Climbing steps: Suppose we want to climb a ladder with n steps. We can do this in two ways:

  • Climbing one single step and then climbing an (n-1) steps ladder
  • Climbing two steps at once and then climbing an (n-2) steps ladder

So, if we call ladder(n)

Get Mastering JavaScript Functional Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.