Loading
Current section: 3. Spread and Rest 4 exercises
solution

Rest Parameters

Transcript

00:00 Alright. Let's jump into it together. We've got our rest parameters. We're gonna start with a function called multiply. Here we go.

00:07 And this is going to take any number of numbers. So this is an array of numbers. There you go. Thank you, l l m. We don't need to have this return type specified necessarily.

00:17 And here it's using numbers dot reduce. This is a function we haven't covered yet. We'll get to that later. So yes. Thank you, AI.

00:23 We will go with the for loop instead. So really what this is doing is it's saying, hey. Go ahead and take any number. I can say, multiply well, multiply two three four five four seven, whatever. And I just keep going on forever, and all of those will be slurped up into a single array called numbers.

00:44 Back in the day, we used to have to do this crazy thing called arguments, and we'd have to splice it and to turn it into an array because arguments is not an array. It's like this whole thing. Yeah. Don't have to do that anymore. It's a lot easier, a lot more explicit what's going on.

00:57 And, yeah. From there, you can just do a four loop over that, multiply them together, and then you can return the product. Tada. And that is that's your rest parameters. So let's continue with a couple other things.

01:09 I actually do functions like this all the time. Function log with prefix, and there we go. So we've got our prefix. There's our string, and we can take any number of messages. So your prefix will be the first thing, and then any additional items in, that arguments list or any additional parameters that are passed, will be slurped up into the messages array.

01:33 Now the interesting thing about this is we couldn't say have, like, a suffix. Great job or whatever. Right? There's no way to do a suffix, after this, this rest because, it's nondeterministic how many items are going to be in there. So this isn't allowed.

01:52 You cannot do anything after your rest. It literally is the rest of them and no more no less. So there's no way to do that. If you wanted to do that, then you would have to do a suffix, here and then the rest of them would be there. So if I wanted my suffix to be great job, then we do that, and then you'd say your suffix goes right there.

02:13 So if that's what you wanted to do, you could do it that way. Okay. Great. There we go. Info done.

02:19 Great job. There you go. Okay. Great. And hold on a second.

02:24 Takes a prefix, takes the rest, logs each one with the prefix. Okay. Yeah. Logs each one. I was thinking that it would slurp them together, but that's the instruction said log each one.

02:33 I forgot. Okay. Great. So let's, continue. We wanna create a function, sum, that takes all of the numbers, returns a sum, and returns zero if no arguments are provided.

02:46 So, you can actually, this is kinda cheating because if there are no arguments, then this, iteration will not, go at all and so the sum will actually work just fine. And actually, it's suggesting we rename sum to total so we don't mix it up with the function name, which I think is reasonable. So we'll do that. But the point of this one I wanted to show you is that it's just a regular array. So you can say if number's length is zero, just return zero.

03:12 And as I said, I like to do early returns. So there you are. Yeah. So it's just a it's a normal array. It's exactly what you'd expect.

03:22 Okay. Great. Now we're going to merge arrays. So, function, merge arrays, and it's an array of arrays. So we're gonna take, and this is actually a little bit of a preview of something that we're gonna do a little bit later.

03:38 We're gonna take any number of arrays and those arrays are numbers. And we're gonna make the merged array. We'll iterate over those arrays. And for each one of those, we get an array, and we're gonna push the contents of that array. And this is getting mad at us because, oh, we didn't type this array properly.

03:58 So we need to type this as a number, an array of numbers. And, yeah, we can make this const. You can mutate, const things. You just can't reassign them. And there we go.

04:08 That's gonna merge our arrays into one. Tada. So So this is gonna giving you a little bit of a preview. This is, also, like, not a preview, I guess. This is the other side of things.

04:19 So we've been accepting, rest params. Now this is using spread with arrays. Now we're spreading this across the push method. So we could say one, two, three, four and instead, we're going to Or actually, we could say to make it more specific to this example, array at zero and array at two and etcetera etcetera. We actually don't know how many there are, and so that's why it's nice to be able to just say dot dot dot.

04:46 Tada. And if this had, like, a prefix thing, here, then we could put the prefix here or whatever. Like, well, that's kind of bad example. But, hopefully, you get the point. You can also do, ah, yeah, etcetera, etcetera, all that stuff.

05:03 We kind of have done this already, so, hopefully, that makes sense. Okay. Great. So then you can do all your console logs if you want, export, and all of that. And now you've got, both.

05:15 You've got, the ability to spread and you've got the ability to take the rest of something. There's actually more to these dot dot dots, that we aren't getting to just yet, but we will get to when we talk about destructuring. Really, really awesome feature. I remember the old days when we didn't have this feature and it was not fun. It's a lot more fun now.

05:33 So there you go. That is rest parameters with TypeScript. Good job.