Building with AI
Debugging for beginners: find the bug, even when AI wrote it
A calm, step-by-step method for finding and fixing bugs in code — reproduce, read the error, narrow it down, fix and re-test — with tips for AI-written code.
By the GenKid team · · Updated · Facts checked · 5 min read
Key takeaways
- Every programmer writes bugs, and so does every AI tool. Debugging is a normal, learnable skill.
- Use a method: reproduce the bug, read the error, narrow down where it happens, form a guess, test the fix, and re-test everything.
- The browser console is your best friend for web apps: it shows errors and anything you log.
- When AI wrote the code, debugging is how you learn what it actually does.
- Asking the AI to "fix it" without understanding often swaps one bug for another.
Bugs are normal
A bug is any time a program doesn't do what you meant. Professional developers spend a large share of their time finding and fixing bugs, and AI tools produce plenty of their own. Getting stuck isn't a sign you're bad at coding — it's what coding mostly is. Debugging is even written into England's computing curriculum, which expects pupils to design, write and debug programs.
The difference between beginners and experienced developers isn't that experienced people write fewer bugs. It's that they have a calm, repeatable method for finding them.
A six-step debugging method
1. Reproduce it
Find the exact steps that make the bug happen. "Sometimes the score is wrong" is hard to fix. "If I answer the last question correctly, the score shows 4 instead of 5" is easy to work on.
2. Read the error message
If there's an error, read all of it. In a web app, open the browser's developer tools and look at the console. Error messages usually say what went wrong and which line it happened on. Even if the wording is confusing, the line number is gold.
3. Narrow it down
Find where the program's behaviour stops matching what you expect. A simple trick is to add console.log lines to print values at different points:
console.log("score before:", score);
score = score + 1;
console.log("score after:", score);If "before" is already wrong, the bug is earlier. Keep moving your checks until you find the line where things go wrong.
4. Make a guess
Say what you think is wrong, out loud or in writing. "I think the last answer isn't counted because the loop stops one question early."
5. Test one change
Change one thing to test your guess. If it fixes the bug, great. If not, undo it — don't pile up random changes.
6. Re-test everything
Fixes can break other things. Run through the main features again, and the edge cases too.
The rubber duck
Programmers have a famous trick: explain your code, line by line, to a rubber duck (or a pet, or a patient parent). Explaining forces you to look at what the code actually says, rather than what you think it says. Surprisingly often, you spot the bug halfway through explaining.
Common beginner bugs
| Symptom | Common cause |
|---|---|
| Nothing happens when you click | The button's id doesn't match the code, or the script didn't load (see how HTML, CSS and JavaScript fit together) |
| "undefined" appears on screen | A variable or property name is misspelled |
| Number is one too high or low | A loop starts or stops one step out ("off by one") |
| "5" + 1 shows "51" | A number is being treated as text |
| Works once, then breaks | Something isn't reset between rounds |
| Error: "X is not a function" | A typo, or calling something that doesn't exist |
Debugging code an AI wrote
AI-written code has a particular challenge: you didn't write it, so you don't have a mental picture of how it works. Debugging is how you build that picture.
A few tips:
- Don't just say "fix it". Asking an AI to fix a bug you don't understand often produces a new bug, or hides the symptom without fixing the cause. In one study of learners aged 10–17, those who handed whole problems to the AI in a single prompt did worst when later asked to change code themselves.
- Describe the bug precisely (it's the same skill as writing a good prompt for coding), using your reproduction steps: what you did, what you expected, what happened.
- Ask for an explanation, not just a fix: "Why does the score show 4? Explain which line causes it."
- Read the AI's fix before accepting it. Use our checklist for AI-generated code.
- Watch for invented things. AI sometimes calls functions that don't exist. If the error says something is "not defined", check whether it was ever written. Here's why AI makes things up.
Stay calm
Bugs are frustrating. If you've been stuck for a while, take a break. Walking away for five minutes really does help, and you'll often see the answer as soon as you come back. Writing down what you've already tried stops you repeating yourself.
GenKid includes "bug hunt" projects, like a dragon egg game with deliberate bugs to find and fix, and lessons that ask learners to explain each bug in their own words. See Level 2 on the pathway.
Frequently asked questions
What does debugging mean?
Debugging is finding and fixing the cause of a program behaving differently from how you intended. It includes reproducing the problem, locating it and testing a fix.
How do I open the browser console?
In most desktop browsers, press F12 (or Ctrl+Shift+I on Windows and Linux, Cmd+Option+I on a Mac), or right-click the page and choose Inspect, then select the Console tab. In Safari you first need to turn on developer tools in Settings, under Advanced. Some learning platforms show a console next to your code.
Should I let AI fix my bugs?
Use AI to help you understand the bug and suggest fixes, but read and test any fix yourself. Accepting fixes you don't understand often causes new problems.
Why does my code work sometimes but not always?
Usually because something depends on the order of events, the starting values, or something not being reset. Find the exact steps that trigger it and check the values at each step.
Sources
- How novices use LLM-based code generators to solve CS1 coding tasks in a self-paced learning environment (opens in a new tab) — Kazemitabaar et al., 2023
- National curriculum in England: computing programmes of study (opens in a new tab) — Department for Education
- What are browser developer tools? (opens in a new tab) — MDN Web Docs
How we write: GenKid articles are researched and drafted with the help of AI tools, then checked in a separate review step against the sources listed above before they are published, and re-checked when they are updated. Rules and products change, so check official pages for your country before acting on anything here. This article is general information, not legal advice. Spotted a mistake? Email hello@genkid.app.