JavaScript array exercises
Arrays are where JavaScript's quiet failures pile up: reading past the end gives undefined instead of an error, sort() compares numbers as text, and an empty array is truthy. Each exercise is a short function with one such bug and tests that catch it.
They range from loop bounds and missing returns to sort, reduce and async callbacks.
The arrays bugs you will meet
- sort() with no compare function sorts as strings: 10 comes before 2.
- arr[arr.length] is undefined; the last item is arr[arr.length - 1].
- filter() always returns an array, and every array is truthy; use some().
- return inside forEach does not return from the outer function.
- map(parseInt) passes the index as the radix.
The exercises
Easy
Medium
- Function crashing on return statementScope and closure
- Sorting numbers alphabeticallyType
- Array mapping returns empty objectsAsync and race conditions
- Deeply nested arrays not flatteningMutation and copying
- Data groups overwriting each otherLogic
- Total ignores the quantity of each itemLogic
- API aggregator dropping responsesAsync and race conditions
- Array contains pending operationsAsync and race conditions
- Counter stuck at zeroScope and closure
- Validation passing empty stringsType
- Number conversion rounding incorrectlyType
- Function crashing when item not foundNull and undefined
- Mapping strings to numbersType
- AI Attempt: Remove all occurrencesLogic
Hard
- Set cleaner gets stuck in a loopInfinite loops
- Event listeners logging identical valuesScope and closure
- Process completes before data arrivesAsync and race conditions
- Sequential data arriving out of orderAsync and race conditions
- Reducer outputting invalid stringsAsync and race conditions
- High-traffic vote counters returning low totalsAsync and race conditions
- Expensive database queries duplicatingAsync and race conditions
- System allowing duplicate reservationsAsync and race conditions
- Class method throwing undefined errorScope and closure
- Search algorithm freezing indefinitelyInfinite loops
- Equality check failing for identical dataType
- AI Attempt: The English ListMutation and copying
Errors you might be searching for
More JavaScript practice by topic
Questions
Are these interview questions?
Some are: the ones titled "AI Attempt" are classic interview problems with a plausible but wrong answer. The rest are everyday bugs.
Is there a certificate?
Yes, free. Fixing 15 easy JavaScript bugs while signed in earns JavaScript Debugging · Level 1.