Chapter 10
These are the solutions to the exercises found in the section Exercises.
-
The base case is if (low > high)—that is, we want to stop the recursion once low has exceeded high. Otherwise, we’d end up printing numbers even greater than the high number, and onto infinity.
-
We’d have infinite recursion! factorial(10) calls factorial(8), which calls factorial(6), which calls factorial(4), which calls factorial(2), which calls factorial(0). Since our base case is if (number === 1), we never end up with number ever being 1, so the recursion continues. factorial(0) calls factorial(-2), and so on.
-
Let’s say low is 1 and high is 10. When we call sum(1, 10), that in turn returns 10 + sum(1, 9). That is, we return the sum of 10 plus whatever the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access