Get an array's iterator and read its first value.
Acceptance criteria
- 1.#out should show 1.
JavaScript Tutorial · JavaScript Generators & Iterators
These 10 tasks go with the JavaScript Iterators chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “An iterator is an object with a next method that hands out one value at a time.” .
1.Call next manuallyexercise · easy · 10 min
Get an array's iterator and read its first value.
Passes when: #out should show 1.
2.Check the done flagexercise · easy · 10 min · practice plan
Drain a one-element array and check the second call is done.
Passes when: #out should show true.
3.Write a custom iteratorexercise · easy · 10 min · practice plan
Build an iterator object that counts to three.
Passes when: #out should show 1.
4.Drain an iterator by handexercise · easy · 10 min · practice plan
Collect every value from a custom iterator into an array.
Passes when: #out should show 1,2,3.
5.Check for the iterator methodexercise · medium · 10 min · practice plan
Report whether a plain object has Symbol.iterator.
Passes when: #out should show no.
6.Fix the value read as the whole resultbug fix · easy · 8 min
next() returns an object; the value is one property of it.
Passes when: #out should show 1.
7.Fix the loop that never stopsbug fix · easy · 8 min · practice plan
Without checking done, the loop keeps calling next past the end.
Passes when: #out should show 3 values.
8.Fix the custom iterator returning a value with done truebug fix · easy · 8 min · practice plan
Once finished, value should be undefined, not a leftover number.
Passes when: #out should show reached the end.
9.Fix the plain object assumed to be iterablebug fix · easy · 8 min · practice plan
A plain object has no Symbol.iterator method, so spreading it throws.
Passes when: #out should show not iterable.
10.Fix done checked with the wrong valuebug fix · easy · 8 min · practice plan
done is a boolean, not the string "true".
Passes when: #out should show is finished.
Between them these tasks cover Iterator protocol, next() and done.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.