Check out a free preview of the full A Practical Guide to Algorithms with JavaScript course

The "Introducing Recursion" Lesson is part of the full, A Practical Guide to Algorithms with JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Bianca introduces recursion, which is when a function calls itself, and its importance in programming.

Preview
Close

Transcript from the "Introducing Recursion" Lesson

[00:00:02]
>> Bianca: Our next topic is my favourite, recursion. And so, at its core, recursion is just when a function calls itself. However, as you may know if you've worked with recursion before, there's a little more to that. Recursion is a technique. It's also a mindset. It's a way of approaching a problem.

[00:00:22]
>> Bianca: [LAUGH] With your head and also you have to feel it in your heart. So hopefully we'll wrap our mind around it and then we'll start to gain some intuition around the recursive technique. And this concept underlies all the rest of this workshop, so ask questions, slow me down to make sure that we're all on the same page so that as we move through the content, no one gets left behind.

[00:00:47]
Recursion is one of those things that's pretty abstract, pretty tricky, so ask lots of questions. I really appreciate it when people do that. All right. So why do we care about recursion? Why do we go through this pain? So it's an elegant solution. It can keep your code D.R.Y. which is do not repeat yourself, which is contrasted with W.E.T. which stands for we enjoy typing or write everything twice.

[00:01:16]
That's a thing that you don't wanna do when you're programming. And it's expected CS knowledge and, believe it or not, it's actually an easier solution sometimes. And we'll get to a point when we get to trees and graphs when it's actually easier to use recursion versus an iterative approach.

[00:01:37]
And here's a common, I don't know. Here's a picture that I like about recursion. So it starts with this question. How many people are in your organization? And then we break this problem into smaller problems, and this guy says me and all our members of department one and department two, etc.

[00:01:55]
And then you talk to the guy in department one and he says me and all members in project one and project two. And that goes all the way down to just me. So we break down the problem all the way to the simplest case, the base case, where it's just one person and then you add all of that work up and you get your result, which is the total number of people in the organization.

[00:02:16]
And so that's sort of the core concept of recursion.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now