62 challenges match these filters
A developer asked ChatGPT to 'write a function that removes all occurrences of a specific item from an array in-place'. The AI confidently provided this code. It passes the basic test removeAll([1, 2, 3], 2), but fails in production.
Logic error
ChatGPT was asked to write a function that formats an array of names into an English list (e.g. ['A', 'B', 'C'] -> 'A, B and C'). The function works perfectly the first time you call it. But the developer who used it noticed their data was disappearing.
Other
parseAll(strings) should take an array of numeric strings and return an array of integers.
Type error
sortNumbers(arr) should sort an array of numbers from smallest to largest.
Logic error
addRounded(a, b) should add two numbers and return the result rounded to two decimal places, as a number. addRounded(1, 2) is 3.
Other
stripDashes(text) should remove every dash, so stripDashes("a-b-c") returns "abc".
Other
isAvailable(item) should be true only when the item is in stock AND not discontinued.
Logic error
percentChange(oldValue, newValue) should report the change as a percentage of the old value. Going from 100 to 150 is +50.
Logic error
joinWithDash(words) should join words with dashes between them, so joinWithDash(["a","b"]) returns "a-b".
Off-by-one
pageItems(items, page, size) returns one page of results. Pages are numbered from 1, so pageItems([1,2,3,4,5,6], 1, 3) gives [1,2,3].
Off-by-one
sumAll(nums) should add up every number in the array, so sumAll([1, 2, 3]) returns 6.
Off-by-one
cityOf(user) should return the user's city, or "unknown" if any part of the path is missing.
Null / Undefined
priceOf(items, name) should return the named item's price, or 0 when there is no such item.
Null / Undefined
greet(user) should greet the user by name, falling back to "guest" when no name is available.
Null / Undefined
retriesFor(settings) should use settings.retries when it is provided and fall back to 3 when it is missing. Setting retries to 0 means 'do not retry'.
Null / Undefined
countUnique(records) should count distinct records by their contents, so two records with the same fields count once.
Type error
describeType(value) should name the type of a value, returning "null" for null and "object" for real objects.
Type error
sumPrices(prices) should total a list of price strings, so sumPrices(["1.5", "2.5"]) returns 4.
Type error
countZeros(values) should count how many entries are the number zero, so countZeros([0, "", false, 0]) returns 2.
Type error
countUppercase(text) should count the uppercase letters, so countUppercase("aBc") returns 1.
Infinite loop
stepsToExceed(limit) counts how many times you must double 1 before passing limit, so stepsToExceed(8) returns 4.
Infinite loop
indexOfValue(sorted, target) should return the index of target in a sorted array, or -1 if it is absent.
Infinite loop
countOdds(nums) should count the odd numbers, so countOdds([1, 2, 3]) returns 2.
Infinite loop
countMatches(words, target) should count how many times target appears, so countMatches(["a","b","a"], "a") returns 2.
Scope error