This course has been updated into a 4 part series! We now recommend you take the A Practical Guide to Algorithms with JavaScript course.
Transcript from the "Looping" Lesson
[00:00:00]
>> off screen female: And your template. What context to you want to put the looper variable or the count variable in? So like if not global where would that fit in your-
>> Bianca Gandolfo: Yeah. Okay.
>> off screen female: Yeah.
>> Bianca Gandolfo: Perfect segue into our next topic. So here is the loop, right. This is a function called loopNTimes.
[00:00:22] We just pass an n and if you're going to keep track of that counter, you keep track of it here, n-1, n+2, however you want to do it, bypassing it through. Cool, okay. So, I'm gonna just go through this to make sure we're all super solid on recursion, because tomorrow, we're gonna be applying this o lots of cool stuff.
[00:00:55] Okay, so here we have loopNTimes. We are going to call it and then I'm gonna pass the mic to the audience, so be prepared. You need to tell me what's gonna happen next and I will do it on the screen. Cool? Okay, who wants to help me out?
[00:01:16] Who is a very helpful person and likes to be helpful? Thanks Andy!
>> off screen male: Sure, anytime. So the first time, it will console dot log N equals three.
>> Bianca Gandolfo: Well, what's the very, very first thing that's gonna happen?
>> off screen male: The very, very first thing? You'll call it.
>> Bianca Gandolfo: Yup and it will pass three, right?
[00:01:37]
>> off screen male: Yes.
>> Bianca Gandolfo: Cool and then you can go from there.
>> off screen male: So you'll call it passing in 3 at console.log and =3, 3 is not less than or =1, so it will not return complete. What it will do is call itself on 3- 1 or 2, in layman's terms.
[00:02:02]
>> Bianca Gandolfo: Cool, all right, who wants to go next? Yeah, okay.
>> off screen female: Okay, so now it'll be called with 2.
>> Bianca Gandolfo: It's gonna be called with 2?
>> off screen female: Yep.
>> Bianca Gandolfo: Okay, so I'm gonna put it on our stack. And just to note, there's a return statement here but we're not there yet, just to be clear.
[00:02:25] Because I think this confuses some people sometimes. But yeah, so n equals 2. So n will console log with 2 and it still isn't less than or equal to 1. So we won't return complete yet, we'll go around for another time, cool. All right who's next? I think Miranda wants to go.
[00:02:45]
>> off screen female: Hopefully.
>> off screen male: One
>> off screen female: [LAUGH] So it'll call itself with n minus 1, or 1, 2 minus 1, 1, yeah.
>> Bianca Gandolfo: Mm hm.
>> off screen female: And then we'll log with 1, if, is less than or equal to one over two. So we'll return complete.
>> Bianca Gandolfo: Return complete. Yeah, absolutely.
[00:03:11] And what that aslo means is that we will not move forward in this function. So once we return, the next thing, the thing that we have to do is, we have to pop this off the star. So here we go, just gonna delete it and that's our pop.
[00:03:28] Everyone clear on how this works? Okay, so I pop it off and this returns us complete, right, in a string, great. So basically and so this return statement is going to return whatever this returns. So it's going to return complete. Pop it off the stack, because this function is done.
[00:03:56] This returns complete again, return statement is just gonna return whatever this function loopNTimes returns and so returns complete. We pop it off and then we have here complete.
>> Bianca Gandolfo: Cool, so if someone sum up in their own words how this process works? Someone who hasn't talked yet. But if no one says anything then I'm gonna call on Abby.
[00:04:41]
>> Bianca Gandolfo: Just like what is your, if you had to summarize or just would have, maybe you have sort of like a concept in your mind of how this works? A picture or something if you're a visual person. Can you share that with me, with the group? This helps everyone here just sort of deepen our knowledge, it helps me know what's going on in your head, as well.
[00:05:01] And I can clarify any confusions, because it's important to make sure we have this solid before we move on tomorrow, when it gets a little trickier. Yeah?
>> off screen male: Can I actually ask a question first?
>> Bianca Gandolfo: Sure.
>> off screen male: What happens to the string complete?
>> Bianca Gandolfo: What do you mean?
[00:05:18]
>> off screen male: So I'm just looking at it in the console and it just gets down to one. But it never Like, where does complete go I guess? Is it just saved for whatever function it goes to next, is that?
>> Bianca Gandolfo: Yeah, that's a great question. So what happens, here, let me just put us back where we were.
[00:05:35]
>> Bianca Gandolfo: So since it's basically anything that's being returned is gonna return to the next up. And so, this function here is the next one that's up, right?
>> off screen female: I get that I just mean at the very end.
>> Bianca Gandolfo: Mm-hm.
>> off screen male: If it's returned, it doesn't print int he console.
[00:05:54]
>> off screen female: It doesn't print in the console, but it's stored somewhere, right?
>> Bianca Gandolfo: Yeah. It's stored in the scope of the function.
>> off screen male: Okay.
>> Bianca Gandolfo: So it's private in there, yeah. Yeah, you can keep track of it by console.logging instead of returning. Or doing both maybe if you wanted.
[00:06:13]
>> Bianca Gandolfo: Cool, okay. So I am still waiting for someone to just give me a summary of this process that we went through in like kind of drawing out and mapping out these recursive calls.
>> off screen female: I guess it the function that calls itself until a specific condition is met.
[00:06:38]
>> Bianca Gandolfo: Yeah, yeah and then what happens when that condition is met?
>> off screen female: It removes itself from the stack, right?
>> Bianca Gandolfo: Yeah, so well, once the function on the stack Has completed, it just hops off the stack and then it goes to the next one, all the way up to the top.
[00:07:07]
>> off screen female: It sounds like you roll your snowball down the hill and it accumulates as it goes? At the end, you just sort of whip the snowball back to where you started. Well, I think you rewind the video [CROSSTALK], [LAUGH]. [LAUGH]
>> Bianca Gandolfo: Yeah, yeah, yeah, exactly. I think of it more as like, you throw the snowball up on top of the hill and then it starts rolling down kind of, you know what I mean?
[00:07:29] Because, or you go all the to the bottom and it rolls backwards against gravity or something. Because it's really going all the way, all the way, all the way down and then it goes back up, does that make sense? Totally.
>> [INAUDIBLE]
>> Bianca Gandolfo: Yeah, exactly, I think that's the most plausible example in my world, that's how my world works.
[00:07:48]
>> off screen male: I guess what I would say is usually try to solve a hard problem by splitting it into simpler problems and if the simpler problem is similar to the original problem Then that's a recursive situation.
>> Bianca Gandolfo: Yeah, yeah. Totally. So we'll see, especially starting tomorrow, exactly what David is talking about here.
[00:08:10] The whole core of recursion is just the same procedure on smaller and smaller bits. And it could save us a lot of time when we talk about fast algorithms. Some of them can save us a lot of time. So, that's kind of cool. We like fast algorithms because that means we can do more cool stuff.
[00:08:33] All right, we're good?
>> Bianca Gandolfo: So, if I was a teacher Hypothetically [LAUGH] I gave you a test tomorrow and was like, hm. How does [LAUGH] recursion, can you solve, can you loop in with recursion,
>> Bianca Gandolfo: What grade would you give yourself? What grade do you think I would give you?
[00:09:03]
>> off screen male: Out of what?
>> Bianca Gandolfo: Out of A through C.
>> off screen male: Okay. [LAUGH]
>> Bianca Gandolfo: There's no failing grades here. A through C. Anyone? Here you can do it. This is A, B and C. It's okay I'm not actually going to give you a test or anything like that. I'm just taking a, I just wanna see where you're at, so I can talk a lot more about this.
[00:09:28] Or, we can get going. It's really up to you, so.
>> off screen male: Is the test to write a recursive function from scratch?
>> Bianca Gandolfo: If I said hey, can you loop n times and write the loop n times specific function. Yes, you guys are good. Awesome, great.