This course has been updated! We now recommend you take the Complete Intro to Computer Science course.

Check out a free preview of the full Four Semesters of Computer Science in 5 Hours course:
The "Exercise 1: Recursion" Lesson is part of the full, Four Semesters of Computer Science in 5 Hours course featured in this preview video. Here's what you'd learn in this lesson:

- In this exercise, you will make a function that computes the factorial of any number recursively. Before starting the exercise, Brian answers a few audience questions about the previous recursion example.

Get Unlimited Access Now

Transcript from the "Exercise 1: Recursion" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Brian Holt: Now I want to go to the exercise here in recursion. So we're going to factorials. There's two punk problems that whenever you talk about recursion everyone always talks about, fibonaccis and factorials. And guess what? You get to do both of them. Okay, so for those of you that are not familiar with factorials, I'm sure you've seen this before five explanation point right.

[00:00:24] And that means 5x4, geez, x3x2x1, or you can leave out 1 because you know, not super useful. So I want you to write a recursive function that does the factorial for you. So a factorial of one is one factorial of two is two factorial of three is six.

[00:00:51]
>> Speaker 2: We've got some questions from the previous example.
>> Brian Holt: Okay.
>> Speaker 2: Winny is asking what is the loop of the for loop equivalent of the Fibonacci function?
>> Brian Holt: I don't know it off the top my hand. You can definitely Google it and maybe while you're doing your exercise, I can take a look at it.

[00:01:12]
>> Brian Holt: Yeah, I can't write it off at the top my head or I'm not going to, it'd take a while.
>> Speaker 2: And then Benjamin H has a question if it's O n log n, would we still drop the modifier n and just say O log n?
>> Brian Holt: That's a great question.

[00:01:30] The answer is no because it's not actually the coefficient. That number grows along with log n and then it actually ends up being the bigger number. So the n part actually is important to that and the key to keep in mind there is that it's still n, right?

[00:01:46] So as you know it grows from 5,000 to 10,000, that n grows along with it. Whereas if it's just three, that three is still three, no matter how many elements you add, that three is still constant. So that's why we keep the n and the log n, because they both grow with all the inputs that we put into it.

[00:02:06] That's what we're looking for. That's the broad stroke we want to know. That's a great question.
>> Brian Holt: Cool, good questions. So here, we're gonna make this factorial function, we talked about that a little bit. And then down here at the bottom, we have unit tests for you. So if you look down here, this is a Jasmine unit test.

[00:02:36] For those of you not familiar with Jasmine, it's a unit testing framework in JavaScript. And it's hopefully give you useful errors. Especially since we're doing recursion, code pen runs your code automatically as you're typing it. So if you can imagine if you're doing incursion and it's running code for you, you're going to get a stack overflow.

[00:02:55] So go ahead and change this to xdescribe. Just put an x in front of it, or you can also put the x here in front of xit. Right? Either one of the that works as well. That will prevent that unit tests from running until you're ready to run it and then you just go delete it.

[00:03:09] And at that point I'll start running it again.