Check out a free preview of the full JavaScript: The Recent Parts course:
The "Iterator & Generator Solution" Lesson is part of the full, JavaScript: The Recent Parts course featured in this preview video. Here's what you'd learn in this lesson:

Kyle live codes the solution to the exercise.

Get Unlimited Access Now

Transcript from the "Iterator & Generator Solution" Lesson

[00:00:00]
>> Kyle Simpson: All right, welcome back. I trust you found that nice and challenging. Let's take a look at how to define an iterator, to make our numbers, object initerable. First we know we need to define a generator, and we're going to put it at this location .Symbol.iterator. And it's a function that we're going to want to receive some inputs.

[00:00:23] But for now let's just skip the inputs. What does that function need to do? Well, it needs to loop over, essentially an i increment here and just yield out the values. So if I said something like let i = 0, i less than 100. i++, we could say less than or equal to 100.

[00:00:44] i++ and then I could just simply yield i. And that generator is gonna spit out the values from 0 up to 100. So if we take that code and let me switch over to our console and we run it.
>> Kyle Simpson: You see it prints out from 0 up to 100.

[00:01:11] Okay, so now let’s talk about we give it some inputs. Well we want to use our named parameter. Our named arguments in text with an object destructuring. So I’m gonna put an object here and make sure to give it the default object so that if it's not passed in, it picks up on the defaults.

[00:01:30] We need essentially three inputs. We need a start which defaults to 0, we need an end which defaults to 100 and we need a step which defaults to 1. So let's use those three variables.
>> Kyle Simpson: Instead of i =, we'll say i += step.
>> Kyle Simpson: Now how are we going to use that number as iteration with our lucky numbers.

[00:02:04] All right, I'm gonna go ahead and switch this to an interpre-literal, and I'll open up an expression. And what we wanna do is have an array that can be printed out. So I'm gonna use an array here and I'm going to be spreading something into the array which is the iterable.

[00:02:26] So I'm going to be spreading numbers but I don't want to just spread numbers itself because that would be 0 through 100. I actually want to call the Symbol.iterator function. And that is what I will be spreading out. Remember iterators are also iterables. So we can spread out a custom made iterator instance, okay?

[00:02:51] So how are we going to call this and pass in those inputs? Well, we're gonna pass in an object that sets start = 6, that sets end equal to 30, and set step equal to 4. So if we take that.
>> Speaker 2: A dollar sign?
>> Kyle Simpson: Dollar sign?
>> Speaker 2: Am I correct?

[00:03:22] For the template literal, am I wrong?
>> Kyle Simpson: Sorry, you're right.
>> Speaker 2: Okay.
>> Kyle Simpson: That's why I wasn't syntax highlighting, good catch, all right.
>> Speaker 3: You need to take everything cuz you have to redefine that function?
>> Kyle Simpson: What's that.
>> Speaker 3: You better copy in the rest of it, cuz you redefine the function on top.

[00:03:38]
>> Kyle Simpson: I redefine the function on top?
>> Speaker 3: We didn't have it taking parameters, last time you copied it over.
>> Kyle Simpson: Yes, you're right, you're right.
>> Kyle Simpson: Okay, let's go over here and we don't need this for loop for right now. So remove that. And let's see what it prints out.

[00:03:58] My lucky number 6,10,14,18,22,26 and 30. If you win $1 billion lottery of those lucky numbers, I expect a 1% cut, so.