Functional JavaScript First Steps, v2

Calculate Factorials with Recursion

Anjana Vakil
Software Engineer & Educator
Functional JavaScript First Steps, v2

Lesson Description

The "Calculate Factorials with Recursion" Lesson is part of the full, Functional JavaScript First Steps, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Anjana shares two factorial function implementations. The interative implementation uses a while loop to complete the factorial operation. The recursive example can be implemented in only a couple of lines but can be difficult for developers to grasp conceptually.

Preview
Close

Transcript from the "Calculate Factorials with Recursion" Lesson

[00:00:00]
>> Anjana Vakil: So what we're going to do in our next exercise, first, we're gonna start with reading some code. Then we're gonna move on to writing some code. So in this case, we're gonna look at alternative implementations of [LAUGH] Every functional programming student's favorite function, the factorial computation. Which if you wanna check out the what factorial is a math class refresher, there's a Wikipedia link, but essentially, we're gonna give it an integer, and it's going to compute the factorial of that integer, right so like
>> Anjana Vakil: Factorial of 3 is what 1 times 2 times 3?

[00:00:39]
Check my math [LAUGH] And so that's what we want this function to compute. Basically that number and all of its smaller numbers multiplied together. Similar to some, but same thing but different. So for right now, let's just take one minute and read through this factorial JS just read it just ask yourself, A, what does each of these functions do?

[00:01:05]
And B, do you find one of them more readable? Easier to understand? Do you find one less readable? Less easy to understand? This is totally subjective but let's take a moment, read through, and we will discuss.
>> Anjana Vakil: So let's take a look at well, first of, do y'all find one of these easier to read?

[00:01:32]
This is a subjective thing, there is no correct answer. Who finds imperative factorial easier to read? Okay, I see a couple hands who finds recursive factorial easier to read? Zero hands, all right why? And again, subjective, we're just talking about our feelings. It's good stuff, crucial to software development, I'm not even kidding.

[00:01:56]
>> Speaker 2: Just like as I'm reading it, it feels like there's a little bit more mental gymnastics. I have to think about like keeping track of how it's changing, and like referencing itself.
>> Anjana Vakil: Okay, in in recursive factorial,
>> Speaker 3: Go to the bottom and then start again and go to the bottom and start again, where, typically, we learn to read left to right, top to bottom here in the west, and that's what a lot of us are used to and so.

[00:02:19]
When you read something that says do this, then this, then this, then this it's like, okay, that's like a recipe I can follow that.
>> Anjana Vakil: Exactly.
>> Speaker 3: Whereas this says, do this and then start over again and do it again it's like,
>> Speaker 2: And then there's an abstract yeah.

[00:02:36]
>> Speaker 3: [LAUGH]
>> Anjana Vakil: Great and so we have to kind of think about that, for example, the order of execution might be a little surprising here, and if you want to, you can mess with this code. You can add, I'll even let you add in pure console logs, just for the sake of learning or what have you to satisfy yourself of like, how this function is doing what it does.

[00:03:01]
But you're right there's a lot of kind of like, we have this sort of almost muscle memory built in, especially as programmers who are used to working with JavaScript. Most of the code that we see is imperative code, largely imperative code, not all of it, like I said, callbacks are everywhere.

[00:03:17]
That's functional cool beads but if we're used to reading while loops and for loops, something new is gonna feel a little weird. Hopefully by the end of this course, and certainly by the end of the rest of the courses in the functional programming path, it will feel a lot more natural to be working with recursion instead of while loops in this case.

[00:03:43]
But there is a certain element to which recursion is just sort of one of those things that makes sense to a functional program more than it might to a human brain. So that's why we're gonna spend so much time practicing it.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Start a 7-Day Free Trial