JavaScript Lazy Sequences — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Lazy Sequences 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
Take from an infinite generator
easy10 minUse a take helper to get the first five natural numbers.
- 2
Cycle through a list forever
easy10 minTake seven values from a repeating three-item cycle.
- 3
Stop safely with break
easy10 minBreak out of a for of loop over an infinite generator after one value.
- 4
Filter a lazy sequence
easy10 minTake the first four even natural numbers.
- 5
Confirm nothing is precomputed
medium10 minReport that only one call to next has happened so far.
Find the bug(5)
- 1
Fix the take helper reading the wrong property
easy8 minThe value is on .value, not the result object itself.
- 2
Fix the cycle indexed without wrapping
easy8 minWithout the modulo, the index runs past the end of the array.
- 3
Fix the loop with no break
easy8 minCapping the generator itself is the wrong fix — break out of the loop instead, so the sequence stays infinite and reusable.
- 4
Fix the filter yielding every value
easy8 minThe condition to keep only even numbers was never applied.
- 5
Fix the array built ahead of time
easy8 minPrecomputing every value defeats the point of laziness — count how many are actually produced.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace