Check out a free preview of the full Functional-Lite JavaScript course:
The "Exercise 4 Solution Part 2" Lesson is part of the full, Functional-Lite JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Kyle continues his demonstration of the solution to exercise 4. He creates an addn() function that takes an array of values and adds them together using the add2() function.

Get Unlimited Access Now

Transcript from the "Exercise 4 Solution Part 2" Lesson

[00:00:00]
>> [MUSIC]

[00:00:05]
>> Audience: Should we not call it add insight though?
>> Kyle Simpson: We all calling it add right here, on line 12. Okay? Alright, now it starts to get more interesting and a little more challenging. We'll see how close you've been paying attention. And I want you cuz, we have this add2 function which takes two functions.

[00:00:33] And if I wanted to be able to take three functions I could make and add three, or an add four, or an add five. But, I want you to make an add and I want you to make a function that takes a list of an arbitrary number of functions and adds all of those function return results together but that function has to use add to.

[00:01:00] There's a variety of ways that you could define your add in. First let's just leave the add2 here so that we see that in comments. There's a variety of ways to define the add in that we're talking about. But what I want you to do is, instead of it receiving parameters, it's going to receive a single array.

[00:01:21] So I'll give you that hint. It's gonna receive a single array. That array is going to have one or more, actually 0 or more, but we don't really care about those trivial cases, functions in it. And you need to go through and calculate all of the sums of all those things using only add2.

[00:01:40] Well, of course you're gonna have to call add2 quite a few times, right? So you might choose to set this up as a for loop. And that might be the first way that you wanna write it. So try it with a loop first. Try it yourself, setting up your addn function to calculate it with a loop.

[00:01:57] And then you'll notice the README says, after you've done it with a loop try it with recursion. And finally, might try it with map and reduce. So here's what I'm gonna do. I'm gonna stop talking for like five minutes, I'll let you try it with a loop first.

[00:02:15] And maybe move on to your recursion attempt at it. But then we're gonna talk through looping, recursion, and then, how we might use map and reduce. So add in. I might have slightly misspoken before I gave you the work break, we want this to receive an array of values, not an array of functions.

[00:02:36] But either way solution is going to be mostly the same. So if you sent in an array of functions, there's just one less step to do. If you are going to send in an array of values, there is one extra step to do. Either way. Let's assume an array of values is what we're gonna do.

[00:02:53] So let's assume that we're gonna call something like addn, and we're going to pass in the values 10 and 42 and 56. And maybe one other value like 73, okay?
>> Kyle Simpson: So we expect that to give us 181 as the sum. How are we gonna do this with a loop, with an iteration?

[00:03:19] Well of course, we're gonna need to keep track of it. We're gonna need an accumulator. We'll call it sum, starting out at 0. We'll say for var i = 0. i less than arr.length, i ++. I;m going to take the entry in array, and I'm going to call it with foo so that's going to produce me a function wrapping around the value, that's the step you don't need to do if you are assuming you have already received function, so either way.

[00:03:56]
>> Audience: Actually I did have to, to do the accumulator.
>> Kyle Simpson: So if you already have a function then that step won't be necessary, but what we're going to have is the sum is going to need to call the function that it gets back so what we're going to do is say.

[00:04:20] I'm sorry not call the function we are going to say add to, and one of the things we are going to need to pass in is the current sum wrapped in a function. So we are going to need to say fusum. So that particular part is optional if you are already passing in functions.

[00:04:42] This part is not optional. Okay? And sum is equal to add2. Okay. You could have also stepped through the loop two at a time. Passing two elements in the loop in and getting their some and just doing some plus equals and add to and passing those in that's also an equally valid way of solving it with the loop.

[00:05:17] But at the end of the day, we can simply return sum.
>> Kyle Simpson: Okay? Well what about if we wanted to do this with recursion? In recursion we're looking for a base case. And what's our recursive definition for the addition of a list? The addition of a list, is the addition of the first two items plus the addition of everything else.

[00:05:53] So it's the item, plus everything else, and then it's the item plus everything else. That's how we add those two together. So, our base case would be if the array that we've been given is length too. Because then we know what to do with that. So array.length that's 2, and you could for good measure if you wanted to, say less than or equal to 2.

[00:06:19] But if we were given an array of at least, in this base case of at least having two in it, then all we need to do is call, add2, and if we've been given a set of values. Of course we're going to need to wrap those. So, we're gonna need to say foo(arr

[0]) and foo(arr[1]).

[00:06:44] You don't need those foo calls if you're passing in the functions already prerunned. What's our Recursion case. We're gonna say return.
>> Kyle Simpson: And we're going to call arr

[0] as a function.
>> Kyle Simpson: All right. And then add in
>> Kyle Simpson: So this is add and, we're gonna need to have an array that has, sorry we're not gonna call it.

[00:07:22] We're gonna need to add in an array, pass in an array. The first element in the array is going to be what was in array position 0. And the second one is going to be everything else in the list. Okay. So, that's going to be array.slice1. Now add in returns us back a number so we're gonna get the numbers and numbers and numbers coming back and back and back to us.

[00:07:57] If we're dealing with, I'm getting a little bit ahead of myself so I'm gonna try to not jump too far ahead.
>> Kyle Simpson: I don't mean to pass this separately, that is not what I meant to do. I meant to call apply, that's why I'm getting ahead of myself.

[00:08:22] So we wanna pass in a single, we wanna spread out in a single array with that as the first value. And then add2 in it. I'm sorry. Getting a little bit off-track, I think I got my brain wrapped wrong. So let me just double-check here. Right. There's that function.

[00:08:53] Okay, let's start back here. Gonna call add in with a single array. Where the first element in the array is a function that returns. Add2 with arr of 0 and arr of 1. So that's our first element. That's an array with a single element in it
>> Kyle Simpson: And we wanna concatenate that with everything else that's at the starting position too.

[00:09:52] So I've taken the two items off the front of the list, passed them to add to and replaced them with a single calculation for it. And then i'm going to keep doing that recursively. So I concatenate that with the rest of the list. Arr.slice2. Should be what I show.

[00:10:12] Yep. So, Single array with a function in it, that function is going to calculate the addition of the first two elements in the list. And then we're going to pass the rest of the arguments in. So, when if we do that the first time and we're gonna take, we're gonna go from a list of four elements to a list of three elements.

[00:10:37] Where the first item in the list is a function and the rest of the elements are either the value or the wrapped functions depending on which one you did. We've gone from four down to three. We're going to do it again, where we take that function and add in another function, we go from three down to two.

[00:10:55] Then we're going to hit this space case which will simply pass in those functions and get the value back out.
>> Audience: On 22 [INAUDIBLE].
>> Kyle Simpson: The reason we don't want if you don't want you're going to be calculating the add to now, so you could do it that way.

[00:11:15] What I'm trying to show is that creating a function that sort of differ the calculation until the final end point.
>> Kyle Simpson: So we can also observe, as I have done here in the comments, that, and the other way of writing this is we only need to do the recursive call.

[00:11:38] If the array length is already greater than 2. Okay?