Progress (0%)

# Iteration Quiz

Quiz 3 16 questions

Prerequisites for the quiz

1. The chapter Introduction to Iteration
2. The concept of Iterators and Iterables
3. The concept of Generators

16 questions to solve

Instructions
1. This quiz goes to full-screen once you press the Start button.
2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
What does the iterator protocol say?
According to the iterable protocol, an iterable object is one with an `@@iterator()` method available. True or false?
Does the object `o`, shown below, conform to the iterable protocol?
``````var o = {
a: 10,
b: 30
}``````
Clearly NO! The reason is because `o` doesn't have an `@iterator()` method available on it. For more info, refer to JavaScript Iterables.
Arrays and strings are iterables, by default. True or false?
One can indeed use arrays and strings in a `for...of` loop which justifies the fact that arrays and strings are iterable by default. For more info please refer to JavaScript Iterables.
What will the following code log?
``````Number.prototype[Symbol.iterator] = function() {
return this.toString()[Symbol.iterator]();
}

var num = 13756, arr = [];

for (var n of num) {
arr.push(n);
}

console.log(arr.join("-"));``````
To fully understand the code here, please refer JavaScript Iterables.
What will the following code log?
``````var num = 13756, arr = [];

for (var n of num) {
arr.push(n);
}

console.log(arr.join("-"));``````
Since numbers in JavaScript are NOT iterable by default, using them in a `for...of` loop will lead to an error. Refer to JavaScript Iterables for more info.
The `for...of` loop requires that you pass it an iterable object. True or false?
Internally, a `for...of` loop invokes the `@iterator()` method on the provided object - which means it operates on iterable objects only. This goes with choice (A). For more info please refer to JavaScript Iterables.
According to the iterator protocol, the `done` property of the object returned by `next()` is of what type?
`done` is of type Boolean, and it indicates whether the iterator has been consumed completely or not. Refer to JavaScript Iterators for more info.
What will the following code log?
``````var arr = [1, 3, 5];
var iter = arr[Symbol.iterator]();

console.log(iter.next());``````
`iter.next()` returns the object `{value: 1, done: false}`. To understand where these properties come from, please refer to JavaScript Iterators.
Are numbers in JavaScript iterable, by default?
Clearly NO! We can NOT iterate over numbers using a `for...of` loop, which is clearly shown in JavaScript Iterables.
What will the following code log?
``````var arr = [1, 3, 5];
var iter = arr[Symbol.iterator]();

iter.next();
iter.next();
iter.next();

console.log(iter.next());``````
To understand this code please refer to JavaScript Iterators.
What will the following code log?
``````function* gen() {
yield "OK";
}

gen().next();
console.log(gen().next());``````
Calling `gen().next()` returns the object `{value: "OK", done: false}`, owing to the `yield` keyword in line 2. To understand this code in detail please refer to JavaScript Generators.
What will the following code log?
``````function* gen() {
yield "OK";
}

console.log(gen.next());``````
`gen()` is a generator function, not an iterator, and therefore calling `next()` on it will throw an error. The correct expression would be `gen().next()`. See JavaScript Generators for more info.
A generator function returns what?
A generator function returns an iterator object that has an `@@iterator()` method defined and is therefore an iterable too. See more at JavaScript Generators.
The `yield` keyword pauses execution. True or false?
The `yield` keyword indeed pauses execution when it's encountered by the interpreter. Details of how the pausing mechanism works are given in JavaScript Generators.
What does the following code log?
``````function* gen() {
yield* [1, 10, 2];
}

console.log(gen().next());``````
The `yield*` keyword (with the asterisk `*` symbol) expects an iterable object and yields each value at a time. This means that `gen().next()` here, will return the object `{value: 1, done: false}`. For a detailed description of `yield*`, refer to JavaScript Generators.

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage