In this exercise, you must 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.
(light music plays) - Now I'm gonna go to the exercise here,in recursion. So we're gonna do factorials. There's two problems that, whenever you talk about recursion everyone always talks about. Fibonacci's and factorials, and guess what? You get to do both of them. So for those of you that are not familiar with factorials, I'm sure you've seen this before, 5!, right? And that means five, times four, jeez times three, times two, times one, or you can leave out one, because, you know, not super useful.
So, I want you to write a recursive function that does a factorial for you. So factorial of one is one, factorial of two is two, factorial of three is six. - [Host] We've got some questions from the previous example. - Okay. - [Host] Reeny is asking, what is the for loop equivalent of the Fibonacci function? - I don't know it off the top of my head. You can definitely google it, and while you're doing your exercise I can take a look at it.
Yeah, I can't write it off the top of my head, or I'm not going to, it'd take a while. - [Host] And then Benjamin H has a question. If it's O(Nlog(N)) would we still drop the modifier N and just say it's O(log(N))? - That's a great question. 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.
So as it grows from 5,000 to 10,000 that N grows along with it whereas if its just 3 that 3 is always three, is still 3. No matter how many elements you add, that 3 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. That's what we're looking for, that's the broad stroke we want to know. But that's a great question.
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'. That works as well. That will prevent that unit test from running until you're ready to run it and then you just go delete it. And at that point it'll start running it again.
Note: This course was created by Frontend Masters. It was originally released on 07/12/2016. We're pleased to host this training in our library.
- Big O
- Sort: Bubble, insertion, and merge,
- Median values
- Set, map, stack, and queue
- Array lists and linked lists
- Binary search tree
- AVL tree
- Single rotation and double rotation
- Hash table
- Functional programming
- Map, reduce, and filter