TryBugHunt
goType errorEasy

Typing letters in the quantity box gives the wrong error

BugFind the bug and fix the code

What it should do

parseQuantity(input) turns the text from a quantity box into a number. Text that is not a whole number gives the error "quantity must be a whole number"; zero or less gives "quantity must be at least 1". parseQuantity("3") returns 3.

What happens instead

parseQuantity("abc") returns the error "quantity must be at least 1" instead of "quantity must be a whole number".

package main

import (
    "errors"
    "strconv"
)

func parseQuantity(input string) (int, error) {
    qty, _ := strconv.Atoi(input)
    if qty <= 0 {
        return 0, errors.New("quantity must be at least 1")
    }
    return qty, nil
}

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the type errors pattern

Integer division that drops the fraction, overflow, and conversions that change the value.

What you'll practise

An easy Go debugging exercise about type errors: integer division that drops the fraction, overflow, and conversions that change the value.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

strconv.Atoi returns 0 together with an error when the text is not a number. Discarding the error with _ leaves only the 0, which then looks like a real quantity of zero, so the user is told the wrong thing. The same happens with "2.5". In Go, the error is the only signal that the value beside it is meaningless; check err before you look at the value. Writing _ for an error is a choice you should be able to justify.

The pattern behind it: type errors.

More type errors to practise

Embed this challenge on your site

Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.

<iframe src="https://trybughunt.com/embed/80bad8b9-a8ce-46c0-8506-0bc10cd2bede" title="Debugging challenge: Typing letters in the quantity box gives the wrong error" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/80bad8b9-a8ce-46c0-8506-0bc10cd2bede">TryBugHunt</a></p>