Eye Training · Loops and Iteration
For every loop, check four things: where it starts, where it stops, what changes each time, and what's left over at the end.
Part 2 · Surface 21. Bug counts are hidden and some snippets are clean. See Start Here for how to use these drills.
J · The basics
Bug hunt
J · Sum the prices
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Email everyone
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Find the admin
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Skip comments
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Fetch every page
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Countdown
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
M · Three years in, these should jump out
Bug hunt
M · Remove expired items
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Insert in batches
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
M · Orders with customers
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Hide blocked users
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Index by id
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Retry three times
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
S · Where seniors stand out
Bug hunt
S · Drop expired sessions
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
S · Process the queue
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
S · Summarise a big file
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
What your eye checks
Whenever you see a loop, ask:
- Bounds: does it start at the first item and stop at the last?
<=againstlengthand starting atlengthare the classics. - What does the loop variable hold?
for…ingives keys,for…ofgives values,entries()gives both. - Does every path move forward? A
continuebefore the increment, or a variable that's never reassigned, means it never ends. - Is the collection changed while iterating?
splicein a forward loop skips items. (MapandSetdeletion duringfor…ofis safe.) - What's left over after the loop? A last partial batch, a final buffered line.
- Does
returnorbreakdo what's intended? Not insideforEach. - Is there I/O inside the loop? One query per item is N+1; sequential awaits multiply latency.
- What's the cost per pass?
includes,findor spread inside a loop makes it quadratic. - How long does it run without yielding? A big synchronous loop in a server blocks every other request.
- Can one item stop the whole loop? Retries without a limit, failures without a dead-letter path.
Tells
| You see | Ask |
|---|---|
i <= arr.length / i = arr.length | One too far? |
for (const x in array) | Keys or values? |
return inside forEach | Who receives it? |
continue in a while | Was the counter updated first? |
while (x.cond) where x is const | Does it ever change? |
splice(i, 1) in a forward loop | What gets skipped? |
| A batch flushed at a size limit | What about the rest? |
await + query inside for | N+1? |
.includes( / .find( inside .filter( | Quadratic? |
{ ...acc, …} in reduce | Quadratic? |
| A failure path that doesn't advance | Poison message? |
| A loop over millions in a request handler | Who else is waiting? |
Go deeper
- The Event Loop Under Pressure: why one long loop freezes every request
- L3 · Data and SQL: N+1 queries and the fixes in bigger codebases
- Previous surface: Objects and arrays · Next: Functions, this and closures