Report the type of an array's Symbol.iterator method.
Acceptance criteria
- 1.#out should show function.
JavaScript Tutorial · JavaScript Generators & Iterators
These 10 tasks go with the JavaScript Iterables 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 iterable is anything with a Symbol.iterator method, which is what for...of actually looks for.” .
1.Confirm an array is iterableexercise · easy · 10 min
Report the type of an array's Symbol.iterator method.
Passes when: #out should show function.
2.Loop over a Setexercise · easy · 10 min · practice plan
Use for of to collect values from a Set.
Passes when: #out should show 1,2,3.
3.Catch a non-iterable loopexercise · easy · 10 min · practice plan
Catch the error from looping over a plain object.
Passes when: #out should show TypeError.
4.Spread a stringexercise · easy · 10 min · practice plan
Spread a string into an array of its characters.
Passes when: #out should show 4.
5.Tell iterable spread from object spreadexercise · medium · 10 min · practice plan
Show both kinds of spread working on their own kind of value.
Passes when: #out should show 3 and dark.
6.Fix for of used on a plain objectbug fix · easy · 8 min
Plain objects are not iterable.
Passes when: #out should show TypeError.
7.Fix the spread of a plain object as a listbug fix · easy · 8 min · practice plan
Spreading an object into an array position needs it to be iterable.
Passes when: #out should show not spreadable this way.
8.Fix the Map assumed non-iterablebug fix · easy · 8 min · practice plan
A Map is iterable, yielding key-value pairs.
Passes when: #out should show has an iterator.
9.Fix the string spread miscountedbug fix · easy · 8 min · practice plan
Spreading a string gives one entry per character, not per byte pair blindly assumed.
Passes when: #out should show 5.
10.Fix destructuring used on a non-iterablebug fix · easy · 8 min · practice plan
Array destructuring needs an iterable on the right side.
Passes when: #out should show cannot destructure.
Between them these tasks cover Iterable protocol, for of and Spread.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.