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.
addRounded(a, b) adds two numbers and returns the sum rounded to 2 decimal places, as a number. Example: addRounded(1, 2) → 3.
Other
stripDashes(text) removes every dash. Example: stripDashes("a-b-c") → "abc".
Other
isAvailable(item) is true only when the item is in stock and not discontinued. Example: isAvailable({ inStock: false, discontinued: false }) → false.
Logic error
percentChange(oldValue, newValue) returns the change as a percentage of the old value. Example: percentChange(100, 150) → 50.
Logic error
joinWithDash(words) joins the words with a dash between each pair. Example: joinWithDash(["a", "b"]) → "a-b".
Off-by-one
pageItems(items, page, size) returns one page of items. Pages start at 1. Example: pageItems([1, 2, 3, 4, 5, 6], 1, 3) → [1, 2, 3].
Off-by-one
sumAll(nums) adds up every number in the array. Example: sumAll([1, 2, 3]) → 6.
Off-by-one
cityOf(user) returns the user's city, or "unknown" if any part is missing. Example: cityOf({}) → "unknown".
Null / Undefined
priceOf(items, name) returns the price of the item with that name, or 0 if there is none. Example: priceOf([{ name: "a", price: 5 }], "b") → 0.
Null / Undefined
greet(user) greets the user by name, or as "guest" when there is no name. Example: greet({ name: null }) → "hi guest".
Null / Undefined
retriesFor(settings) returns settings.retries if it is set, or 3 if it is missing. 0 means "don't retry". Example: retriesFor({ retries: 0 }) → 0, retriesFor({}) → 3.
Null / Undefined
countUnique(records) counts records with different contents. Two records with the same fields count once. Example: countUnique([{ id: 1 }, { id: 1 }]) → 1.
Type error
describeType(value) names the type of a value. It returns "null" for null and "object" for real objects. Example: describeType(null) → "null".
Type error
sumPrices(prices) adds up prices that arrive as text. Example: sumPrices(["1.5", "2.5"]) → 4.
Type error
countZeros(values) counts the entries that are the number 0. Example: countZeros([0, "", false, 0]) → 2.
Type error
countUppercase(text) counts the capital letters. Example: countUppercase("aBc") → 1.
Infinite loop
stepsToExceed(limit) counts how many times you double 1 before it is bigger than limit. Example: stepsToExceed(8) → 4 (1 → 2 → 4 → 8 → 16).
Infinite loop
indexOfValue(sorted, target) returns the index of target in a sorted array, or -1 if it is not there. Example: indexOfValue([1, 3, 5, 7], 4) → -1.
Infinite loop
countOdds(nums) counts the odd numbers. Example: countOdds([1, 2, 3]) → 2.
Infinite loop
countMatches(words, target) counts how many times target appears. Example: countMatches(["a", "b", "a"], "a") → 2.
Scope error
Each tracker from makeTracker() starts at 0. trackTwice(a, b) adds a to one tracker and b to a new one, and returns the second total. Example: trackTwice(5, 3) → 3.
Scope error
priceWithFee(base) adds a fee of 5 to the price. Example: priceWithFee(10) → 15.
Scope error
countAll(items) counts the items using a Counter object. Example: countAll([1, 2, 3]) → 3.
Scope error
describe(useLocal) returns "local" when useLocal is true, and the outer label "global" when it is false. Example: describe(false) → "global".
Scope error
collectNames(ids) loads every name, then reports how many there are and the type of the first one. Example: await collectNames([1, 2]) → "2-string".
Async race condition
Showing 76–100 of 133 JavaScript challenges