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" 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:

In this exercise you will create a numbers object that is iterable. This iterable object should be able to iterate one-by-one through a set of number or step through the numbers at a custom interval. The code for this exercise is in the ex5.js file.

Get Unlimited Access Now

Transcript from the "Exercise 5" Lesson

>> [MUSIC]

>> Kyle Simpson: Here's our exercise. You're gonna get to do almost exactly what we just did but in a slightly more sophisticated way, okay? Question, where is the next call? It is inside of the dot dot dot iterator or inside of the for of loop. The dot dot dot iterator and the for of loop automatically consume an iterator until its completion.

[00:00:30] So they automatically loop over it and call the dot next call and pull out its value and all that stuff for you.
>> Speaker 2: So if you accidentally did the on something that never stopped generating, you'd have a stack overflow real fast.
>> Kyle Simpson: That's exactly right. If you called a dot dot dot on a while true generator, you're going to lock up your program.

[00:00:49] It's going to go forever, okay.
>> Kyle Simpson: Exercise five, I want you to define a numbers object that is iterable. And by default it should start at position or at the value at the number zero. Going by 1s up to and including 100. But I also want it to be able to make my own custom iterator which can customize where to start, what increment to use and where to end.

>> Kyle Simpson: So, you're gonna wanna to make yourself an iterator generator here that does this looping and yielding out of numbers and all that other stuff. But you're also going to want to make that thing be able to take arguments. So that you can make a custom iterator if you want to call it manually.

[00:01:53] So this example, example five is designed to actually weave together almost everything that we've talked about today. It's on purpose. Because the correct solution to this will include, obviously, generators. It's gonna include iterators. It's gonna include symbols. It's gonna include concise properties or methods. It's gonna include,
>> Kyle Simpson: Object destructuring in the parameter list.

[00:02:20] It's gonna include defaults. It's gonna include blocks scoping. So all of the things that we've talked about in little bits and pieces fit together in this one little example. Now that might sound like a really daunting exercise. Until I tell you the sum total of lines of code that you need to write to solve this exercise is under ten lines of code, okay?

[00:02:43] So there's not a lot of code to write. You just have to figure out of all the stuff that we've talked about today, how do I bring all those different pieces together to make this, these two for loops work correctly, okay? Yes.
>> Speaker 3: Will you cover what happens when you cull a next with a value?

>> Kyle Simpson: I'm not going to cover that today because that's getting into more advanced generator usage. It sends the value in and replaces the yield expression and stuff but that's beyond the scope of what we're going to talk about today.
>> Kyle Simpson: This is our last and final exercise. That's why it's pulling in everything.

[00:03:23] About the only thing we don't really pull in is template strings. I couldn't figure out a way to use template strings here. Other than that, try to figure out all the stuff that we've talked about, how to weave all that stuff in.