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
- First 3 items comes back with only 2Off-by-one
- First item name crashes on an empty listNull and undefined
- Adding up [1, 2, 3] gives NaNOff-by-one
- Number sort puts 10 before 2Logic
- Adding shipping gives a list of undefinedNull and undefined
- Validator never finds the negative numberLogic
- Digit sum gives "0123" instead of 6Type
- Stock check can't find the first productLogic
- Filtering products by inventory drops items with zero countLogic
- Sorting numbers puts 100 before 25 and alters original arrayLogic
- Math min on empty price list returns positive infinityLogic
- Parsing string numbers with array map returns NaN and binaryType
- Empty cart tax calculation raises TypeError in reduceType
- Array unshift returns new length instead of prepended arrayType
- Helper adding role returns array length instead of arrayType
- Find index returning zero evaluated as falsy in conditionLogic
- Deleting array element leaves empty slot instead of removingLogic
- Array find returning zero treated as not foundLogic
- Array indexOf looking up object returns negative oneLogic
- Math max on empty score list returns negative infinityLogic
- Helper dropping first item returns deleted item with spliceLogic
- Array constructor with map skips uninitialized empty slotsLogic
- Iterating array with for in treats indices as string concatenationType
- Sorting mixed case words orders capital letters before lowercaseLogic
- Flattening nested category list leaves inner arrays unflattenedLogic
- Array shift removes first element and returns removed itemLogic
- Checking all items in empty cart are in stock returns trueLogic
- Array pop on array with zero element returns fallback valueLogic
- Sort comparator returning boolean never returns negative oneLogic
- Overdue warning shows for every library cardLogic
- Search can't find Apple when you type appleLogic
- Adding to the cart returns a numberLogic
- Array check never says yesType
Medium
- Finding the first negative number crashesScope and closure
- Last 2 items comes back with 3Off-by-one
- Sorting puts 9 after 100Type
- Word lengths come back as promises, not numbersAsync and race conditions
- Flattening stops after one levelMutation and copying
- Grouping words keeps only one per groupLogic
- Cart total ignores how many of each itemLogic
- Loading 3 items returns only oneAsync and race conditions
- Names list is full of promises, not textAsync and race conditions
- Counting matches always gives 0Scope and closure
- Zero counter also counts "" and falseType
- 1.5 + 2.5 comes out as 3Type
- Price lookup crashes for an unknown itemNull and undefined
- map(parseInt) turns "10" into NaN and 2Type
- AI Attempt: Remove all occurrencesLogic
- Every team ends up with every playerMutation and copying
- Array sum returns 0012Type
- Order filter with global regex drops every second matchScope and closure
- Array fill with empty array shares same sublist instanceScope and closure
- Identical lists are reported as differentType
- Seat numbers come out emptyMutation and copying
- Getting the top scores reorders the original listMutation and copying
- Admin check says there are no adminsLogic
- AI Attempt: Title case crashes on a double spaceNull and undefined
Hard
- Removing duplicates never finishesInfinite loops
- Every callback reports index 3Scope and closure
- Doubling with forEach returns an empty listAsync and race conditions
- Names come back in finishing order, not input orderAsync and race conditions
- Async sum gives "[object Promise]3"Async and race conditions
- Adding 4 votes at once keeps only the lastAsync and race conditions
- Cached loader runs 3 times instead of onceAsync and race conditions
- 3 people book the last seatAsync and race conditions
- Counter method crashes inside forEachScope and closure
- Search freezes when the value is missingInfinite loops
- Two identical records counted as differentType
- 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.