This course has been updated! We now recommend you take the JavaScript: From First Steps to Professional course.

Check out a free preview of the full JavaScript: From Fundamentals to Functional JS, v2 course:
The "_.reduce() Exercise" Lesson is part of the full, JavaScript: From Fundamentals to Functional JS, v2 course featured in this preview video. Here's what you'd learn in this lesson:

In this exercise, students can pick one of two assignments: implement _.reduce() functions.

Get Unlimited Access Now

Transcript from the "_.reduce() Exercise" Lesson

[00:00:00]
>> You have two options here, you have the challenging exercise or the relaxed exercise. The first exercise is reduce. So reduce is one of those methods like map and filter that are core to the heart of functional programming, and even kind of pseudo functional programming, which is what we're doing.

[00:00:27] And so it's important to understand how to use it. The implementation is tough, but very interesting. Let me tell you how it works. So reduce takes a collection, and it takes a callback function. And this function is going to or reduce always returns one value at the end.

[00:00:57] So map, remember map returns the length of an array the same length. Each doesn't return anything, filter will return based on that truthy test that we pass it. Reduce always returns one function. So what it does is it will call, it does loop though, it loops through the collection, calling that function.

[00:01:19] And then based on whatever that function returns, it gets passed on back and it gets accumulated. So that function is gonna be called each time with the past value of the return of that function and the next value in the loop. Does that makes sense? So as we're looping in, we're calling this function.

[00:01:45] Previously, we always call the function with the current item index list, right? In this case, we're gonna call it with a previous value and the current value. And take those two values and it's gonna return one value together. And the example that everyone uses is addition. Some argue it's because reduce is so confusing that no one can figure out how to do anything else except for addition with it.

[00:02:11] But here it is, so we have a list with two items, 1 and 2. Our callback function is gonna be called with the previous value and the current value. So, the very first go around. And this is the initial value. You can also pass an initial value, so the initial value, Will be passed as an argument, you add them together, 0 plus 1 is 1.

[00:02:38] So sum is now 1, n is then 2. 1 plus 2 is 3. So that's an example how reduce is used. And here's a more complex example kind of. So we have an initial value, which is an object, okay? And our function takes the result value, key. So the result, sorry, the initial value is the result at first, right?

[00:03:24] So we start with this, result is an object. And it says if result at value, value is going to be 1 if the key is a. You guys following? Tell me to stop if I'm going too fast. This is a little tricky. This is a common pattern here to say like if this exists, then initialize it as 0, otherwise, do something else.

[00:03:52] So this says if it exists or if it doesn't exist, yeah, so if result at value is false, is gonna go to the other side of this or, and it's gonna initialize result value, add an empty array. Return that and it's gonna push the key. If it already exists though, it's just gonna return this and then push the key to that.

[00:04:29] Does that make sense what this line is doing? No, okay, we'll do it one more time. So what this is doing is it's collecting all of the keys that have the same value. And, So what it's doing is it's initializing an empty object. And it's first checking, is this value in our object already?

[00:05:04] So, result empty object, the value for the first property for starting here, it's gonna be 1. So we're saying result.1, does that exist? No, it doesn't exist yet. Okay, so then we're gonna go to this side and we're going to initialize result value to be an empty array.

[00:05:27] And then we're gonna push the key. So result value is 1, is an empty array. So we're creating this first array and it's empty at first, and then we're pushing the key and that key is A. And then we'll return that result, right, which is an object that has a 1, and an A, inside of a array, okay?

[00:05:57] And then, We do it again, same object. This time the value is gonna be 2, again it's gonna initialize that, and it creates this, here, returns the result. Now we have something that matches this one and the result that value, which is 1 already. I'm sorry, it matches this one here.

[00:06:22] And so we'll just instead of initializing it, we'll push the key. Okay, and then we return the result. So that's reduce. It's a little tricky. So the challenge, if you want a challenging task, is to implement that under the hood has this sort of like accumulation effect, where it has a memory and it passes it along every single time.

[00:06:54] And then, if you're feeling like it's late, and you wanna relax a little bit, we have eachRight, which is a for each, except for instead of going from left to right, you go from right to left. So it's very relaxed.
>> At this point, yesterday, that was-
>> Seems like you would reverse the array instead of have a function that starts on the right.

[00:07:24]
>> Do you mean that's how you would implement it is you would reverse the arguments?
>> No, I'm just saying it just seems weird for each array instead of doing for each and just reversing the order of the array.
>> Yeah, well, if you reverse the order of the array, if you're thinking about time complexity and stuff, it can be expensive.

[00:07:45]
>> Yeah, it can, if it's a huge array, I guess.
>> Yeah. Yeah, it's just a thing to do if you don't feel like implementing reduce. However, reduce is pretty cool. So I recommend doing that one, even if it's intimidating and I'll walk around and help.