This course has been updated into a 4 part series! We now recommend you take the A Practical Guide to Algorithms with JavaScript course.

Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Recursive Multiplier Solution" Lesson is part of the full, Data Structures and Algorithms in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Bianca continues her demonstration of the solution to the Recursion Interview Questions exercise by implementing the recursiveMultiplier() function. The code for the solution is located on the "solutions" branch in the Github exercise repository.

Get Unlimited Access Now

Transcript from the "Recursive Multiplier Solution" Lesson

>> Bianca Gandolfo: Is there another one that you wanna do?
>> Student1: Five.
>> Student2: Yeah, five.
>> Student3: Five.
>> class: [LAUGH]
>> Bianca Gandolfo: All right.
>> Student2: [LAUGH] I don't actually understand the directions on five.
>> Bianca Gandolfo: Well, hopefully I do, let's see.
>> Student2: It's like I'm the only one.
>> Student3: There's an extra word into.

>> class: [CROSSTALK]
>> Bianca Gandolfo: That takes two arguments return value.
>> Student2: Okay.
>> Bianca Gandolfo: Yeah, I see. Like that?
>> Student2: Yeah, okay.
>> Bianca Gandolfo: Great, array function that takes two arguments, array and number, and multiplies each array value by a number and returns an array of the values. So for example, let's just do some input-output.

[00:00:40] So we have an array of numbers, we pass it through a function and it's gonna transform. And then we also pass maybe three. We're gonna pass it through a function, and it's gonna transform our array into 3, 6, 9. Cool, is this how everyone understood the problem? Great, so this isn't too dissimilar to our other one, right?

>> Bianca Gandolfo: RecursiveMultiplier equals a function, it's going to take an array of numbers and then here we are.
>> Student1: You also need a multiplier on your function.
>> Bianca Gandolfo: Thank you so much. What should we call that? Num is what we're gonna call it. Great, so who here has completed this?

[00:01:34] Cool, let's see. How did you do it?
>> Student3: I pop the last element off a list, ran recursive multiplier again, and then pushed it. So I multiplied it in place. So I said, remove the last element, multiply, and then-
>> Bianca Gandolfo: Tack it on.
>> Student3: And add on it.

>> Bianca Gandolfo: Onto the same array?
>> Student3: Yeah.
>> Bianca Gandolfo: Got it.
>> Student3: That's why I multiplied it in place.
>> Bianca Gandolfo: Cool, cool, cool. So you didn't actually pop it off actually, or you did?
>> Student3: I said, var last equals arr.pop. And then recursive multiplier, call it on the new shorter array and then pushed last times number.

>> Bianca Gandolfo: Okay, let's walk you through.
>> Student3: Yeah, please.
>> Bianca Gandolfo: So what's the first thing that I need to do? I wanna see your solution.
>> Student3: Okay.
>> Bianca Gandolfo: Yeah.
>> Student3: So I said, if arr.length equals 0, return arr.
>> Bianca Gandolfo: Okay.
>> Student3: And I said, var last equals arr.pop. Well here, I'll let you type.

>> Bianca Gandolfo: Okay.
>> Student3: Okay, so var last equals arr.pop. And then, recursiveMultiplier arr, num.
>> Bianca Gandolfo: Okay.
>> Student3: This looks circular, and then arr.push.
>> Bianca Gandolfo: Down here?
>> Student3: Yep, last times num.
>> Student3: And then return arr.
>> Bianca Gandolfo: Okay.
>> Bianca Gandolfo: I see. Okay, cool.
>> Bianca Gandolfo: So let's do our thing that we do with the stack.

[00:03:38] Does that hold full for all of us? Yeah, thumbs on that? Great, so let's take a look at this. Let's pretend our input's one, two, three, and then three. So recursiveMult, I needed to have shorter names, recursiveMultiplier.
>> Bianca Gandolfo: One, two, and three, three, semicolon. All right, so here's the first one, right?

[00:04:13] I'm just doing those brackets to keep track. Here's the first one, and the values that we have here, let's make it a little bit bigger. The values that we have here are 1, 2, 3, and then comma 3. We're all on the same page? So arr.length, here, this is gonna be false so we're gonna skip this.

[00:04:37] Var last is arr.pop, so that's gonna give us the value 3. And our array, just to have a side note is just now 1 and 2, right? So when we pass it to recursiveMultiplier, the things that we're passing are 1 and 2, and then 3, cool. So this is getting us closer to our base case here, when we're popping it off.

>> Bianca Gandolfo: And this is our base case, 1 to 0. Great, so we're gonna pause here,
>> Bianca Gandolfo: And I'm gonna put some stars here just to say, this is where we pause in our code, because we're gonna hop into this function.
>> Bianca Gandolfo: 2, is everyone keeping track of my weird symbols?

[00:05:26] Great, you guys are so smart. Okay, [SOUND]
>> Bianca Gandolfo: Okay, so here we are. We're in one layer, right? So we have, this is 2. We're passing 1 and 2 and then 3, good. So this is still false, because our length is now 2. We're gonna pop off that last one, it's gonna give us 1.

>> Bianca Gandolfo: It's gonna give us 2, and then our function is gonna be 1.
>> Bianca Gandolfo: Cool.
>> Bianca Gandolfo: Another one, my goodness. So we are going to call you 3. Just a reminder of those numbers, we have 1, 3
>> Bianca Gandolfo: It's like inception, just keep going down. So here we just have 1, 3.

[00:06:45] Length is still false. We're gonna pop 1, our array is not empty. Sad, sad array.
>> class: [LAUGH]
>> Bianca Gandolfo: [LAUGH] I gotta entertain myself somehow. So we have our fourth call. And then we're passing an empty array, and then 3. I forgot to do my stars. Remember, the stars are just where we left off, right?

[00:07:17] Cuz whenever we hop into a function, we don't finish executing the next code that's really important. So we're just gonna hop on in.
>> Bianca Gandolfo: My goodness, 5, I got to make these examples shorter.
>> Bianca Gandolfo: Yes, and then comma 3. This is true finally. So now, we're returning our array, right, which is just empty at this point.

>> Bianca Gandolfo: And then, so-
>> Student2: Bianca?
>> Bianca Gandolfo: Yeah?
>> Student2: A request?
>> Bianca Gandolfo: Sure.
>> Student2: Can you change it from plain text to JavaScript in your thing, so that they get the syntax highlighting.
>> Bianca Gandolfo: Sure, [SOUND] beautiful.
>> class: [LAUGH]
>> Bianca Gandolfo: So our array, which is empty, is gonna be returned.

[00:08:17] All right, so now we need to go back up the stack. So this was five, wait, sorry this was four. Guys, you didn't tell me I was wrong. That's your job to tell me when I'm wrong and I'm here to argue with you.
>> Bianca Gandolfo: So we're gonna pop this off, remember, we're returning the array, we're gonna pop this off.

>> Bianca Gandolfo: And now, we are going to return our array. What happens here?
>> Bianca Gandolfo: Return this array in here, but I don't know if anything's gonna happen. What's gonna happen?
>> class: Go down to the next line?
>> Bianca Gandolfo: Yeah, nothing really. So we're gonna go down to the next line, and we're gonna do array.push.

[00:09:10] Array is still empty though in this frame, times num which is 3, and then we're gonna return it again. So the return goes up.
>> Bianca Gandolfo: And at that point actually, where were we? Last.num, one times three. Yeah, so we're returning up. And we're here, in returns. A return is this, but it's actually not doing anything for us.

[00:09:49] Cool, so we get back here. Array.push, last times num which is 2 times 3, which is 6. We return our array, [SOUND] things are getting complicated. Is our array one?
>> class: It's three.
>> Bianca Gandolfo: Or is it three, but why I wonder? Does anyone know?
>> Student1: This was passable last time.

>> Bianca Gandolfo: Yeah, it's actually because we're just doing work all in the same array. So all the work that you're doing through, no matter where we're pushing it, it doesn't really matter. All that really matters is the order in which we're doing this, because we're all doing work in the same array.

[00:10:44] We're not making copies of the array, does it make sense? Cool, so what's happening here when we pushed, I shouldn't have deleted all that stuff. Where are you? Okay, so here, actually, no. That one is okay. We'll get rid of you. This is where the magic happens. Nope, still not happening.

[00:11:12] Here's our magic. So when we're pushing here, we're pushing one. Then when we return this array, we don't need to return anything. We can just return-
>> Student1: No, we're not pushing one, we're pushing three.
>> Bianca Gandolfo: Yeah, three, thank you. We can return, [SOUND] here it doesn't really matter.

>> Bianca Gandolfo: And we go up, it's all about the order in which this is executed. The next one we're gonna push, and it's gonna be 3 comma, what's the next one?
>> class: Six.
>> Bianca Gandolfo: Six, great. And again, we had popped this off. We're continuing and returning the array which is fine.

[00:11:55] And then, since we did that we've popped that. Did we do this one yet? Are we one less, did we skip one? We should have a 9 here somewhere.
>> Student3: Isn't it this time?
>> Bianca Gandolfo: Did we not just do this one?
>> Student3: No.
>> Bianca Gandolfo: Okay. And then you're right, cool.

[00:12:21] Here we are, we're gonna push it. We're gonna return our array, which is awesome because now it matters. And we have 3, 6, [SOUND] that's not 6 and 9. Beautiful solution, and then we pop this off because you are dead.
>> class: [LAUGH]
>> Bianca Gandolfo: You're dead to us, recursiveMultiplier.

[00:12:45] That was too many layers of recursion for one 3:30 in the afternoon.
>> class: [LAUGH]
>> Bianca Gandolfo: [SOUND] Cool, question?
>> Student2: It's a request. If you're tired now, this is gonna make you work. Do it without a wrapper function, please.
>> Bianca Gandolfo: Do it without a wrapper function. So this is without a wrapper function.

>> Bianca Gandolfo: This example is without a wrapper function.
>> Student2: Maybe it was the previous one, I might have missed it.
>> Bianca Gandolfo: Okay. Yeah, so this is an example of it happening without a wrapper function. So it's important to be mindful here, that we can do what we're doing here with pushing this array, because it's an array.

[00:13:31] It also works with objects. Whenever we're mutating data for a primitive data type, like a string or a number, this isn't gonna fly, yeah? This is when we need to return it directly like we were doing previously when we were returning string loop. Because what we're doing when we have primitive data types is we're passing copies around pretty much.

[00:13:52] When we're doing objects in arrays and things like that, or actually just objects in arrays and functions, cuz that's an object, but whatever. Those are going to stay the same, you're not passing copies, you're just passing pointers to it. Does that make sense? Cool.
>> Bianca Gandolfo: All right, great.

[00:14:18] Any questions about this?
>> Bianca Gandolfo: No? All right, do you guys wanna do some stack and queue stuff now that we left off, or do you have a question?
>> Student1: Did you say that there is a more complicated way to do the recursive reverse one or this is a different way?

>> Bianca Gandolfo: Yeah, it's in the same style as this.
>> Student1: Okay.
>> Bianca Gandolfo: Yeah, yeah, it's not. I don't know if it's more complicated necessarily maybe I said that, but it's just the order in which things happen it just is less linear seeming than this, right? Does that make sense?

>> Student3: Cuz you push it to the front, right? Instead of pushing it to the end it-
>> Bianca Gandolfo: On shift, yeah.
>> Student3: I mean, for the recursive reverse, if you wanna do it my style.
>> Bianca Gandolfo: Yeah, great. So this is recursive in the same order. So you would just need to unshift instead of push.