Data Structures and Algorithms in JavaScript

# Recursive Reverse Solution

Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Recursive Reverse 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 begins walking through the solution to the Recursion Interview Questions exercise. The first question she answers is how to implement a recursiveReverse() 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 Reverse Solution" Lesson

[00:00:00]
>> Bianca Gandolfo: We're gonna go over some solutions, talk about the coming days. How to prepare mentally, physically, all those things, for building on this knowledge, right? So today is mostly your view of some concepts that the rest of the course is going to build on, yeah? So even if you don't master it today, you're gonna see it again.

[00:00:25] And we're just gonna keep building on it and building on it. These little building blocks, right? Just like this. Let's start with some recursion problems. We'll go through some of them. Is there one in particular that you like?
>> Bianca Gandolfo: Which one?
>> Speaker 2: The last one.
>> Bianca Gandolfo: RecursiveReverse.
>> Bianca Gandolfo: Awesome.

[00:00:52]
>> Bianca Gandolfo: So I'm just gonna take that instruction, put it here. So our task is to write a function called recursiveReverse that takes an array and uses recursion to return its contents in reverse. I like this, so let's just set it up, recursiveReverse equals a function. Probably takes some array, yeah?

[00:01:21] There we are, okay. So there's a few ways we can do this, right? The first way, the easiest way probably is to have a function. I'm sorry, to have an array, a second array that we just pushed in to backwards, right? And so, we've been looping in the previous problems, looping using recursion.

[00:01:42] So the simplest way is just to create an array and loop through it backwards, yeah? And then the other way is by swapping. Cool, let's do it the easy way first.
>> Bianca Gandolfo: So let's create our second array.
>> Bianca Gandolfo: Let's see, var addItems, we'll call it. So, who here got to this problem?

[00:02:25] Cool, did anyone do it this way with the loop, or with the extra array?
>> Speaker 2: We didn't finished the problem.
>> Bianca Gandolfo: You didn't finish it. Okay, no worries. Here we are. Cool, so the basic setup. We probably wanna, actually this would be, we'll call this reversed array, right?

[00:02:51] It's a better name. And we'll return at the end the reversed array. So I'm just doing some setup. What we're gonna do is we're gonna loop backwards using recursion, and we're gonna push into this array
>> Bianca Gandolfo: We're gonna use this function here to do that, this is gonna be a recursive function on the inside.

[00:03:12] And then at the end, we're just gonna return our reversed array. Sound solid? I think it's sounds solid, that's just me. All right, so you have to call addItems, right? Probably wanna pass it the initial array just to start it off.
>> Bianca Gandolfo: Then,
>> Bianca Gandolfo: Just to be clear, we don't actually need to pass it in, but just for clarity.

[00:03:49] Let's see, what's the opposite of reverse? Like, inOrderarr?
>> Speaker 2: OrderedArr.
>> Bianca Gandolfo: Ordered, okay.
>> Speaker 3: It's reversed.
>> Bianca Gandolfo: It's what?
>> [INAUDIBLE]
>> Speaker 3: Never mind.
>> Bianca Gandolfo: [LAUGH]
>> Bianca Gandolfo: It's over my head. That's my kind of joke too, just like super cheesy. I mean that as a compliment.
>> Speaker 2: [LAUGH]

[00:04:25]
>> Bianca Gandolfo: Okay, so I just named this ordered array, just to be clear that It's not. It technically is the same and you could do it this way. But when you start naming things the same, and it starts to get confusing about which one is which. So that's why I named it differently.

[00:04:38] Great, all right. So here we are at the meat of the problem. Let's take a stab. What is our,
>> Bianca Gandolfo: Base case?
>> Bianca Gandolfo: What could be a base case in this scenario?
>> Speaker 2: First array?
>> Speaker 3: Like lesson two? Ideally.
>> Bianca Gandolfo: Yeah, so like we can say if, assuming we're pushing off a bit, right, or popping off a bit.

[00:05:17] If array is empty, return, cool. Yeah, anything else? So addItems, so we're going to our recursion here. We need to get closer to an empty array.
>> Bianca Gandolfo: So what should we do? How do we get there? How do we get there?
>> Speaker 2: Of the end of it.
>> Bianca Gandolfo: [SOUND] So we say order, so orderedArr.

[00:06:01]
>> Speaker 2: Is the-
>> Bianca Gandolfo: Dot pop.
>> Speaker 2: Is the array that you're taking, ordered array then? Is the array that you're taking in, ordered array?
>> Bianca Gandolfo: Yeah, this is our original array that's not reversed yet.
>> Speaker 2: Where's order, [INAUDIBLE]
>> Bianca Gandolfo: Yeah, it's just a parameter name. So it comes in here and then I pass it down here.

[00:06:23]
>> Speaker 2: Got you.
>> Bianca Gandolfo: And then it becomes that. Cool, so we pop it off. So we're getting closer, but what else do we need to do?
>> [INAUDIBLE]
>> Bianca Gandolfo: Yeah, reversedArr.
>> Bianca Gandolfo: Cool.
>> Bianca Gandolfo: So this is our base case. We're just gonna say as long as we have a length, as long as it is greater than 0, we're gonna do these things.

[00:07:37]
>> Bianca Gandolfo: Else return. This look like it's gonna work? Do we have any bugs in this code?
>> Bianca Gandolfo: Did I spell anything wrong?
>> Bianca Gandolfo: Does return need to be in an else, like will that evaluate every time otherwise?
>> Bianca Gandolfo: It will, but it doesn't matter at this point because that it's at the end.

[00:08:18]
>> Bianca Gandolfo: Cool, should we try it? There's only one way to know if it works.
>> Bianca Gandolfo: All right, let's check it out.
>> Bianca Gandolfo: All right, so let's just put it in order and let's see.
>> Bianca Gandolfo: What do we think? [SOUND] I'm so nervous. [SOUND] It is reversed. So that's an easy way of doing it.

[00:08:51] And I always recommend don't over complicate your life unnecessarily. Don't optimize before you need to. So if this is doing what you need to do and you have a small data set, I say rock on with this. Cool, and then this is the wrapper function style that I was mentioning before.

[00:09:16] Where we have a function in here, we have a variable in an enclosure that we're just adding to. It's a lot easier to keep track of what you're returning,
>> Bianca Gandolfo: In this case. So if you can do it that way, awesome. It's a little less elegant than some other styles.

[00:09:38] But-
>> Speaker 2: What's a closure again?
>> Bianca Gandolfo: A closure is just like a, closure is about scope and it happens when you have a function inside of a function. And so, the reason that this is called a closure variable, because this inner function has access.
>> Bianca Gandolfo: To retains access to it, and it's also inside of another function.

[00:10:08]
>> Bianca Gandolfo: Normally you do some funky things with returning this addItems outside of this function. So that you can actually access reverse array through addItems even if that function had already executed, which is why it has a fancy name. In this case, it's just scope. You know what I mean?

[00:10:31] Not really anything special about it.