Exercise on Iterators and Generators

We will play around with Iterators and Generators to get a deeper understanding of how they work.

These exercises help you explore how iterators and generators work. You will get a chance to play around with iterators and generators, which will result in a higher depth of learning experience for you than reading about the edge cases.

You can also find out if you already know enough to command these edge cases without learning more about iterators and generators.

Exercise 1:

What happens if we use a string iterator instead of an iterable object in a for-of loop?

let message = 'ok';
let messageIterator = message[Symbol.iterator]();

messageIterator.next();

for ( let item of messageIterator ) {
    console.log( item );
}

Solution

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.