This course has been updated! We now recommend you take the JavaScript: The Recent Parts course.

Check out a free preview of the full ES6: The Right Parts course:
The "Exercise 5 Solution" Lesson is part of the full, ES6: The Right Parts course featured in this preview video. Here's what you'd learn in this lesson:

Kyle walks through the solution to exercise 5. The solution is located in the file ex5-fixed.js.

Get Unlimited Access Now

Transcript from the "Exercise 5 Solution" Lesson

>> [MUSIC]

>> Kyle Simpson: Let's take a look at this exercise five. It might be useful for me to start from the bottom which is to think about, what would it look like for me to call a custom iterator to create a customized iterator. It might be the case that I would have the numbers object, at symbol.iterator, we know that's a function, so I can pass in some arguments to it.

[00:00:30] And what arguments are in a pass in? I wanna pass in the value of where it should start, the increment, and where it should end. I could do that as three separate arguments, but we learned earlier about object and structuring and named arguments and things like that. So, I'm gonna use an object and I'm gonna pass in a start value of six, and increment, I'll call it step value of 4.

[00:00:59] And an end value of 30. So I used an object to do the named arguments. Everybody with me so far?
>> Kyle Simpson: So now, we know that if it's called by default we're gonna need defaults for all of these, but if it's not called by default it's going to allow us to customize.

[00:01:19] By specifying one or more of these arguments. So now we wanna put an iterator on it. We're gonna need to make a generator that is a computed.
>> Kyle Simpson: Concise method. We're gonna talk about the parameter list in just a moment. But there's our syntax for making a concise computed generator.

[00:01:49] Now what about the parameter list? Well, we probably wanna do object destructuring, since we're passing in an object. Before I even do the destructuring, don't forget to put the default equals because this one will fail if we don't. Now let's account for those three parameters that we want including the defaults, we want a start that defaults at 0.

[00:02:17] We want a step that defaults to 1. We want an end that defaults to 100. I'm gonna go ahead and pull those out into separate lines, just so we keep a little more readable. So that's the signature for our iterator generator. Have I lost anybody yet? If you got to this point the rest is easy, okay?

[00:02:45] Cuz all we have to do now, is do a for loop starting at start, going until we get to end incrementing by step. Whatever that value is simply yield it out.
>> Kyle Simpson: That's it. We could make that a let. Doesn't really need to be a let here but.

[00:03:15] It is a way to stylistically signal to a future reader, we intend for I to only be used in the context of the loop, don't try to use it before or after.
>> Kyle Simpson: I'd probably leave this one as a var, we are already using the let's on these for loops.

>> Kyle Simpson: Let's double check it, make sure that it works correctly.
>> Kyle Simpson: So it went from 0 up to 100 and then it went from 6 to 30, incrementing by four.
>> Kyle Simpson: All right, there's a question about the syntax. Remember in de-structuring the default is we use an equal to declare the default, so this is not an object.

[00:04:05] This is an object de-structuring pattern, that's why we're not using the colon.