ES6: The Right Parts Exercise 2 Solution
Transcript from the "Exercise 2 Solution" Lesson
>> Kyle Simpson: So let's talk about solution for Exercise 2. We have the a1 and the a2 arrays. If we wanna pass those in, we can just spread out a1 and a2. And then we have the foo function, which we want to define. We want it to return something, but we're gonna declare the parameters in such a way that it does the work for us.
[00:00:27] So we basically want to account for, we wanna keep the first one, but then we've got two that we don't. We don't care about the value, 4, or the value, 6. So essentially, those are ones that we're gonna throw away. So we can name them y and z, or we could name them something else that indicates that we're going to ignore them, right?
[00:00:46] We could call them ignored one and ignored two if we wanted to. But then everything else we do care about. So we can say ...rest, or args, or whatever you wanna call it. So now we have an x value, and we have the rest value. And we wanna put those two together.
[00:01:00] So we can say return, make ourselves an array that includes x and everything else.
>> Kyle Simpson: Now the foo name here can throw people off because what would foo be? You'd give foo some sort of a name that made sense. For example, if the whole purpose of this function is to ignore the second and third arguments, then you might call this thing ignore23 or something like that.
[00:01:35] You'd give it some sort of reasonable name that described what it was doing. And now this would be a utility. And ostensibly, this would be something that you were doing on a regular basis. So now you can use this facility to pass in a set of values. And have it ignore just the 2 and the 3 and keep everything else, right?
[00:01:55] So don't get too tripped up when you see tricks like this, and you see me only using foo and bar. Think about it in terms of how would I use that kind of facility in my own code? What would I call that utility, that sort of thing?