Check out a free preview of the full Functional-Light JavaScript, v2 course:
The "Challenge 7: Solution 2" 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:

Building off the previous solution, Kyle changes the addn() function to now work with an array of functions instead of an array of values creating an appraoch that allows the incorporation of the reduce() method. - Tthere's a horn that starts at 04:53:33:20 and lasts till 04:58:35:13

Get Unlimited Access Now

Transcript from the "Challenge 7: Solution 2" Lesson

[00:00:00]
>> Kyle Simpson: Okay, so that's our iterative approach. And that algorithm should have come through because what we're basically saying is take the list and make it a little bit smaller each time. That actually leads us to the idea of what the algorithm would be if we did it recursively.

[00:00:16] We could do the same thing just make a smaller and smaller list and make the recursive call, not do it with a while loop. And we won't need to do the block scoping destructuring part because that part can be taken care of with the recursive call. So what I'll do is comment this out and then re-implement addn recursively.

[00:00:40] And what I'm gonna do is take advantage of,
>> Kyle Simpson: The signature to go ahead and do that for me. And then I want to say, if, so my base case is if fnslength = 0, actually let me say, if it's greater than 0, meaning that there's still stuff to do.

[00:01:06] Then we need to go ahead and,
>> Kyle Simpson: Do a recursive call.
>> Kyle Simpson: And the array, sorry this is not, let me do the destructuring here.
>> Kyle Simpson: The array that we wanna pass in is gonna look exactly the same. It's gonna have a function that includes fn0 and fn1.

[00:01:43]
>> Kyle Simpson: And then it's gonna include the rest of the fns in it.
>> Kyle Simpson: So that's the array being passed to our recursive call. And if we're not in that base condition then we know that fn0 and fn1 are the last two to deal with. So we can just simply call add2 with fn0 and fn1.

[00:02:10] Same algorithm as what we did before, but now we're expressing it recursively instead of iteratively. So if I didn't make another huge mistake, let's try that out and see if it works.
>> Kyle Simpson: And there we get 35.