BugHunt

Logic errors: code that runs perfectly and answers wrongly

The wrong operator, an inverted condition, a formula the right way round but backwards.

14 free challenges8 Python6 JavaScript

What is logic error?

The program does exactly what you wrote, and what you wrote is not what you meant. There is no error message and no crash — only a wrong answer, which is why these are the hardest to spot and the most likely to reach production.

Why it happens

Boolean logic and arithmetic are easy to invert without it looking wrong. || instead of && lets either side pass. any() instead of all() accepts one match instead of requiring every one. A reversed subtraction flips the sign of every result while the magnitude stays plausible. Short-circuit guards placed after the thing they guard read as safe while doing nothing.

How to recognise it

  • The magnitude is right but the sign is wrong.
  • It is correct for uniform input and wrong for mixed input.
  • A guard clause exists but the crash it prevents still happens.
  • Boundary values are handled inconsistently at each end.

Errors and symptoms this causes

  • wrong output but no error
  • and vs or in condition
  • any vs all
  • result has the wrong sign
  • condition always true

How to fix it

Build a small truth table for any condition with more than one term, and test each branch. Check one case per direction for signed results. Read guards in evaluation order, not reading order — `and` short-circuits left to right, so a guard on the right protects nothing.

Practise logic errors

Working code with one bug in it. Find it, fix it in the browser, and see the explanation. No account needed.

pythonEasy
Word count breaks on double spaces

count_words(text) should count the words in a string, however much whitespace separates them.

Logic error

javascriptEasy
isEven that returns the opposite

isEven(n) should return true if n is even, false otherwise.

Logic error

javascriptEasy
Free shipping threshold is off by a cent

qualifiesForFreeShipping(total) should return true when the order total is 50 or more — spending exactly 50 qualifies.

Logic error

pythonEasy
Checkout allowed without valid payment

can_checkout(cart_total, has_valid_payment) should return True only when the cart has items AND payment is valid.

Logic error

javascriptHard
Availability check uses or instead of and

isAvailable(item) should be true only when the item is in stock AND not discontinued.

Logic error

pythonHard
Guard checked after the division it protects

safe_ratio(a, b) should return True when a / b is greater than 1, and False when b is zero rather than crashing.

Logic error

javascriptMedium
Grouping keeps only the last match

groupByLength(words) should group words by their length, so ["ab","cd","x"] gives {"1":["x"],"2":["ab","cd"]}.

Logic error

pythonMedium
Discount is subtracted as a flat amount

checkout(total, pct) should apply a percentage discount, so checkout(200, 50) returns 100.

Logic error

pythonMedium
Leap year check gets century years wrong

is_leap_year(year) should return True for leap years: divisible by 4, except centuries, unless divisible by 400.

Logic error

pythonMedium
Grade boundaries return the wrong letter

grade(score) should return "A" for 90+, "B" for 80-89, "C" for 70-79, "D" for 60-69, and "F" below 60.

Logic error

pythonMedium
Gaps between readings come out negative

largest_gap(nums) should return the biggest jump between consecutive values in an ascending list, so largest_gap([1, 5, 6]) returns 4.

Logic error

pythonMedium
any where all was meant

all_positive(nums) should return True only when EVERY number is greater than zero.

Logic error

javascriptMedium
Percentage change comes out backwards

percentChange(oldValue, newValue) should report the change as a percentage of the old value. Going from 100 to 150 is +50.

Logic error

javascriptMedium
Total ignores the quantity of each item

cartTotal(items) should total price x quantity for every item, so two of a 10 unit item plus one of a 5 unit item is 25.

Logic error

Other bug patterns