JavaScript Iterators — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Iterators lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Call next manually
easy10 minGet an array's iterator and read its first value.
- 2
Check the done flag
easy10 minDrain a one-element array and check the second call is done.
- 3
Write a custom iterator
easy10 minBuild an iterator object that counts to three.
- 4
Drain an iterator by hand
easy10 minCollect every value from a custom iterator into an array.
- 5
Check for the iterator method
medium10 minReport whether a plain object has Symbol.iterator.
Find the bug(5)
- 1
Fix the value read as the whole result
easy8 minnext() returns an object; the value is one property of it.
- 2
Fix the loop that never stops
easy8 minWithout checking done, the loop keeps calling next past the end.
- 3
Fix the custom iterator returning a value with done true
easy8 minOnce finished, value should be undefined, not a leftover number.
- 4
Fix the plain object assumed to be iterable
easy8 minA plain object has no Symbol.iterator method, so spreading it throws.
- 5
Fix done checked with the wrong value
easy8 mindone is a boolean, not the string "true".
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace