Functional-Light JavaScript, v2

# Challenge 6: Solution

This course has been updated! We now recommend you take the Functional-Light JavaScript, v3 course.

Check out a free preview of the full Functional-Light JavaScript, v2 course:
The "Challenge 6: Solution" Lesson is part of the full, Functional-Light JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

## Kyle walks through the solution to Challenge 6.

Get Unlimited Access Now

Transcript from the "Challenge 6: Solution" Lesson

[00:00:00]
>> Kyle Simpson: Okay, let's talk about our practice with recursive ideas, in Exercise 6. [COUGH] As you see, the read me says, we've got this mult function, we wanna turn it into a recursive function. If you look at the existing implementation, it was very straightforward for three numbers, but it would not have worked for 4, 5,6 numbers.

[00:00:20] We wanna create a recursive one that would work appropriately for any number of them. So I'm gonna start by.
>> Kyle Simpson: Listing our product, because in multiplication we call it a product instead of a sum. Our product and then our nums. collecting the rest of them up in the nums, [COUGH] and that's the same signature I'd use even if I was going to do the iterative forms, so it doesn't even really need to change its name to say that it's recursive.

[00:00:53] This is how we would want to express it. Ideally, recursion is a hidden detail from the user of your function. Ideally, they shouldn't have to care about that. All right, if I were to write a loop, I would just start out my four-loop and just start multiplying each item in the numbers list into the product, I'd just say, times equals into the product and then return that.

[00:01:15] So let's start then by expressing this as a recursive implementation. In the same way for the numbers where we were adding, we can express the iteration through recursion by saying, we're just gonna pair that numbers list down one at a time. So our base case is when the numbers list is at zero.

[00:01:36] When its length is at 0. And if that is the case, then whatever was passed in as product is what we wanna return.
>> Kyle Simpson: If not, if we're not in the base condition, we're gonna need to make a recursive call, and the recursive call is gonna be product* what?

[00:01:58]
>> Speaker 2: Call to the function.
>> Kyle Simpson: The call to the function, the recursive call to the function.
>> Speaker 2: With the nums array.
>> Kyle Simpson: With the numbers array. The numbers array's already been pared down. It's already the tail, so we don't need to do anything else to it. We just need to pass it along and say, go figure out what the product of that is.

[00:02:13] Come back and give me that answer. We'll multiply that by the current product, and then we're done. If you like the word result better than product, you can also use that. It's more generic than sum and product.
>> Kyle Simpson: Let's test that real quick, just to make sure that's gonna work appropriately.

[00:02:35]
>> Kyle Simpson: And there we go, we get 360. Which matches with mathematic intuition, because 3, 4, 5 is 60 so 3, 4, 5, 6 would be 6 times that 360.
>> Kyle Simpson: Questions about exercise six?
>> Speaker 2: So, I did a similar thing to the last one you did where I took the first 2 values.

[00:03:01]
>> Kyle Simpson: We're gonna get to that in just a sec, I was just gonna show that. I just wanna make sure there weren't any questions about the algorithm first.
>> Kyle Simpson: As you were just saying, which is great. Let's, in a forward thinking way, and again that won't be totally obvious yet.

[00:03:19] But in a forward thinking way, let's peak ahead into our tail, let's look at the next item in the list. So I'll just name that num. And then.
>> Kyle Simpson: My base condition is still the same, but I need to multiply product by num. And then add num back into the list of things that's gonna be passed along, if we aren't in the base condition.

[00:03:47] Does that match with how you did it?
>> Speaker 3: Yeah, a little bit differently.
>> Kyle Simpson: Okay.
>> Speaker 3: I checked to see if num was undefined. If it was a returned x, otherwise I return the call to mult with the first argument as product times num, and then the tail.

[00:04:12]
>> Kyle Simpson: Let me see if I understand.
>> Speaker 3: But it's the same sort of.
>> Kyle Simpson: You're saying your base condition was if num is undefined?
>> Speaker 3: Yeah, I just popped it in chat if you wanna.
>> Kyle Simpson: Does that represent what you were doing?
>> Speaker 3: Yeah.
>> Kyle Simpson: All right, so.

[00:05:17] [COUGH]
>> Kyle Simpson: We are gonna come back to the approach that you're actually hinting towards. So rather than me comment too much on it, let me just pause that part of our discussion. I see what you're doing with that, and that's a good thing that you're doing it that way, but you're just slightly ahead of where we're headed with our discussions, so let's just pause that for a moment.