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

The "Factorial with a Loop" 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 looks at how the factorial algorithm would be implemented with a for() loop. The loop starts at the number 2 and continues to multiply the numbers together until the desired factorial is reached.

### Transcript from the "Factorial with a Loop" Lesson

[00:00:00]

>> Bianca: Factorial, we talked a little bit about factorial. And I was like, it does some work, don't worry about it. Now we're gonna worry about the work that it's doing each time. So a factorial is always gonna take the previous result and times it by the next one up.

[00:00:15]

So what does that look like? So a result equals 1 times 2, 2 times 3. I'm sorry, it's the multiplication of the result times the next thing. So this would be, it's gonna take the last one. Let me just zoom in for you. So the very first calculation is 1 times 2.

[00:00:37]

The next one is the previous calculation times the next value, which is 3. Take the previous calculation, which is 6 times 4, so 2, 3, 4, 5. And then the first number's always gonna be the previous one. So it's gonna be just like how it prints. So this is with a loop.

[00:01:01]

This is the iterative solution of a factorial. And then, we also have it recursively. And so something to keep in mind is identifying these patterns, what is the pattern? How is this problem breaking down over time? And you may notice that, essentially, it's the current number, plus the computed factorial of that number minus one, right?

[00:01:35]

>> Bianca: Let's take a look at how this runs. So returning 5 times the factorial of 4, etc., etc., etc.

>> Bianca: And this is a classic recursive example. And also, the example that I like to point out is actually easier to just do with a loop. That's my perspective.

[00:01:57]

I think it's easier. Okay, I'm gonna keep going with these looping things. All right, so here's one, okay. Here's a couple of different ways that we can do it. So we have just our loop, right? Here's a function. It takes a starting point and an ending point. And we're just gonna make a loop.

[00:02:26]

And we're just gonna log the numbers between some start and end point. So we start at whatever the starting point is, in this case, it's one. And then we only live once really until we get to two, which is the end. And we're gonna console.log each time. Pretty simple, that's our loop, and here it is using recursion.

[00:02:48]

So notice that we have a function in here, okay? So we're gonna console.log, star in the end. We're defining this function, but we're not calling it yet. Let's play the call stack game, why not? Let's just do it. It's gonna be fun. Okay, play the call stack game.

[00:03:15]

>> Bianca: Okay, so what is this? Okay, I see.

>> Bianca: Okay, we're gonna call that function.

>> Bianca: If we could focus. So we're gonna call this function. Our start and end is going, I'm gonna remove our console logs just to give us some space here. So the start and end is gonna be 1 and 3.

[00:03:57]

So we're gonna recurse on i. However, this function hasn't been called yet, so we're not even gonna go there. We're getting ahead of ourselves. We have to wait until we call it. We're calling this function, not this function, okay? So we're gonna take that function and we're gonna put it on the call stack.

[00:04:19]

We're calling it with our start, which has the value 1, okay? So i is now 1. If i < end, remember we can reach up into our parent. We keep track of n, which is 3. Which is is i < end?

>> Speaker 2: Yes.

>> Bianca: Yeah, uh-oh, it's happening again.

[00:04:45]

Let's start keeping track of where we're leaving off before we get lost. So then we need to call that function again. i is i + 1, which is 2. We can do that kinda math. Okay, so i is 2, and we're reaching back up, what is it? 3, is it less?

[00:05:06]

Yes, we're gonna do it even more. Okay, and then now we're hitting 3. Is 3 less than 3? No, so we don't enter into this. We have that implicit return. It's gonna return undefined.

>> Bianca: So this is returning undefined, right? So we win here. This is returning undefined.

[00:05:35]

Then, we continue executing this function from where we left off. There's nothing else going on, except that this also implicitly returns undefined. Cool, we pop that off. Same here, implicitly returns undefined, we're gonna pop it off. And same, implicitly returns undefined, we pop this off. And meanwhile, we would be console logging hitting index at i.

[00:06:06]

>> Bianca: So recursive looping, pop this guy off.

### Learn Straight from the Experts Who Shape the Modern Web

- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops