Loading 3 items returns only one
loadAll(ids) loads every id and returns all the results in order. Example: await loadAll([1, 2, 3]) → [10, 20, 30].
Explore 133 JavaScript challenges. Filter by bug pattern or difficulty.
bookSeats(n) sends n booking requests at the same time for 1 remaining seat, and returns how many bookings succeeded. It should never be more than 1. Example: await bookSeats(3) → 1.
Async race condition
countLoads(n) calls a cached loader n times at once and returns how many times the slow load really ran. It should always be 1. Example: await countLoads(3) → 1.
Async race condition
doubleLater(value) waits a moment, then gives back value × 2. Example: await doubleLater(4) → 8.
Async race condition
getName(id) loads a user and gives back their name. Example: await getName(1) → "user1".
Async race condition
loadAll(ids) loads every id and returns all the results in order. Example: await loadAll([1, 2, 3]) → [10, 20, 30].
Async race condition
tallyVotes(votes) adds up every vote. The votes are counted at the same time with Promise.all. Example: await tallyVotes([1, 2, 3, 4]) → 10.
Async race condition
safeDivide(a, b) returns a / b, or -1 if the division fails. Example: await safeDivide(10, 2) → 5, await safeDivide(10, 0) → -1.
Async race condition
sumAsync(nums) adds up values that are fetched with an async function. Example: await sumAsync([1, 2, 3]) → 6.
Async race condition
labelAll(items) returns the names in the same order as the input, however long each one takes. Example: await labelAll([{ name: "a", ms: 20 }, { name: "b", ms: 1 }]) → ["a", "b"].
Async race condition
cartTotal(items) adds up price × quantity for every item. This project has two files: cart.js and lineItem.js. Each item looks like { price: 10, quantity: 2 }. Example: [{ price: 10, quantity: 2 }, { price: 5, quantity: 1 }] → 25.
Logic error
groupByLength(words) groups words by their length. Example: groupByLength(["ab", "cd", "x"]) → { 1: ["x"], 2: ["ab", "cd"] }.
Logic error
capitalize(word) makes the first letter a capital. Example: capitalize("debug") → "Debug".
Off-by-one
flattenDeep(arr) removes all nesting, however deep. Example: flattenDeep([1, [2, [3, [4]]]]) → [1, 2, 3, 4].
Other
addPrices(a, b) adds two prices and returns a result correct to the cent. Example: addPrices(0.1, 0.2) → 0.3.
Other
sumDelayed(nums) adds up numbers that each arrive after a short delay. Example: await sumDelayed([1, 2, 3]) → 6.
Async race condition
doubleAll(nums) doubles every number with fetchDouble, which takes a moment, and returns the results. Example: await doubleAll([1, 2, 3]) → [2, 4, 6].
Async race condition
wordLengths(words) returns the length of each word. Example: await wordLengths(["abc", "defg"]) → [3, 4].
Async race condition
collectIndexes(n) starts one delayed callback for each index and returns the indexes they report. Example: collectIndexes(3) → [0, 1, 2].
Scope error
countVotes(entries) adds up the votes of every entry. The votes arrive as text, like "3". Example: votes "3" and "4" → 7.
Type error
finalPrice(price, isMember) gives members 10% off and returns the price to pay. Example: finalPrice(100, true) → 90, finalPrice(100, false) → 100.
Scope error
sortNumbers(nums) returns the numbers from smallest to largest. Example: sortNumbers([10, 9, 100, 1]) → [1, 9, 10, 100].
Type error
dedupe(arr) returns the array without duplicates, keeping the first copy of each value. Example: dedupe([1, 2, 2, 3]) → [1, 2, 3].
Infinite loop
reverseString(text) returns the text backwards. Example: reverseString("abc") → "cba".
Infinite loop
qualifiesForFreeShipping(total) returns true when the order is 50 or more. Example: qualifiesForFreeShipping(50) → true, qualifiesForFreeShipping(49.99) → false.
Logic error
firstItemName(items) returns the name of the first item, or "None" when the list is empty. Example: firstItemName([]) → "None".
Null / Undefined
Showing 101–125 of 133 JavaScript challenges