Understand & Fix the Code
AI writes code fast. But you publish it, so you are responsible for it. Learn to read AI code, find bugs with free browser tools, and ask AI for fixes the smart way.
In this chapter you will
- Understand why you should never paste AI code without looking
- Read a real web page and know what each part does
- Use your browser's DevTools to find errors
- Spot the most common AI mistakes and fix three real bugs
- Write a bug report that gets a good answer from AI
Why you should never paste code blindly
AI can write a whole web page in seconds. It feels like magic. But the AI does not see your page, it does not test it, and sometimes it guesses. If you copy its code without looking, four things can go wrong.
Hidden bugs
AI code often looks perfect but has small mistakes: a missing tag, a typo, a button that does nothing. If you don't check, your visitors find them first.
Security and privacy
Your page's code is public: anyone can right-click and choose View page source. AI might put a password or a secret key in it (like the Mistral key you use in DigiBot), and then everyone can copy it. Or it adds a form that collects emails with no protection.
You learn nothing
If you only copy and paste, you depend on AI forever. Every explanation you read makes your next project faster and easier.
You can't fix it later
Next month something breaks. If you know which part does what, you change one line. If not, you ask AI to rewrite everything, and it may break something else.
Good news: you don't need to write code. You only need to read it well enough to ask good questions. It's like reading a rental contract before you sign it: you don't need to be a lawyer, but you should know what's inside.
Read a real page, line by line
Below is a complete mini website for a film night. It has 42 lines, just like a page an AI would write for you. It uses the three languages from Chapter 1: HTML for content, CSS for style and JavaScript for actions.
Click any highlighted line (or a numbered dot) to see what it does in plain English, and what happens if you delete it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1">
<title>Film Night · Erasmus Club</title>
<style>
body { font-family: sans-serif; margin: 0;
text-align: center; }
.hero { background: #1f1140; color: white;
padding: 40px 20px; }
.btn { background: #f5a300; font-size: 18px;
padding: 12px 24px; border: 0; }
@media (max-width: 600px) {
.hero h1 { font-size: 28px; }
}
</style>
</head>
<body>
<header class="hero">
<h1>Film Night 🎬</h1>
<p>Friday 9 October · 19:00 · Room B12</p>
</header>
<section>
<h2>Join us!</h2>
<p>Free popcorn. Bring a friend.</p>
<button class="btn" id="join-btn">I'm coming</button>
<p id="message"></p>
</section>
<script>
const button = document.getElementById("join-btn");
const message = document.getElementById("message");
function sayThanks() {
message.textContent = "See you there! 🍿";
}
button.addEventListener("click", sayThanks);
</script>
</body>
</html>
-
<!DOCTYPE html>is always the very first line. It tells the browser: “This page uses modern HTML.” Visitors never see it.If you delete it: the browser switches to an old “quirks mode” from the 1990s. Sizes and spacing can suddenly look strange.
-
Everything between
<head>(line 3) and</head>(line 19) is information for the browser, not content for visitors.Line 4,
charset="utf-8", lets the page show every letter and emoji: ü, ç, ş, é, 🎬.If you delete the charset line: special letters can turn into strange symbols, like
üinstead ofü. -
This tag tells phones: “Use your real screen width.” It is split over two lines, and that's fine: HTML doesn't care about line breaks inside a tag.
If you delete it: phones pretend to be a big desktop screen and show your page tiny and zoomed out. Your phone rules on line 15 stop working properly. Always keep this tag!
-
The text inside
<title>appears in the browser tab, in bookmarks, in search results and when you share the link. Keep it short and clear.If you delete it: the tab shows the file name or web address instead, for example
film-night.html. It looks unfinished. -
On line 21,
class="hero"is like a name tag on the header. On line 11,.hero(with a dot!) means “style everything that wears the name tag hero”. That's how CSS and HTML connect.The names must match exactly:
hero,Heroandherosare three different names.If you delete or misspell
class="hero": the header loses its dark background and white text. There is no error message. It just looks plain. -
A CSS rule starts with a selector (
.btn= “which elements?”). Then come curly brackets{ }with property: value; pairs inside.#f5a300is a hex color code: a color written as 6 characters (here: amber).18pxmeans 18 pixels, the size of the letters.If you delete a
;: the browser can't tell where one pair ends and ignores the next pair. Delete the}and the rules after it can break too. -
@media (max-width: 600px)means: “Use the rules inside only when the screen is 600 pixels wide or less.” That's phones. Here the big title gets smaller so it fits.If you delete it: the page still works, but on a phone the long title may break into ugly lines or stick out of the screen.
-
id="join-btn"(line 28) gives the button a unique name. On line 32,document.getElementById("join-btn")means “find the element with the id join-btn”.Class or id? Many elements can share a class (for style). An id must be used only once per page.
If the two names don't match: JavaScript finds nothing and the button does nothing. This is one of the most common AI bugs. You'll hunt it further down!
-
function sayThanks() { … }is a list of steps with a name. When the browser reads it, nothing happens yet. It waits until someone calls the function.Line 36 is the only step: put the text “See you there! 🍿” into the empty paragraph
<p id="message">from line 29.If you delete it: line 39 calls a recipe that doesn't exist. The Console shows a red error:
sayThanks is not defined. -
addEventListener("click", sayThanks)means: “When someone clicks this button, runsayThanks.” An event is something that happens on the page: a click, a key press, scrolling.Notice:
sayThankshas no()here. We hand over the recipe; the browser runs it later, at the moment of the click.If you delete it: the page looks exactly the same, but clicking the button does nothing.
New words? The Glossary explains them all in plain English.
Let AI be your teacher
You can't remember every tag, and you don't have to. When you see code you don't understand, ask AI to explain it, not to rewrite it. Copy a prompt and paste your code under it.
Shortcut in DigiBot Studio: the Explain button Open your site in DigiBot Studio and turn on Click to edit. Click the part of your page you're curious about (for example the menu or a button), then press Explain. DigiBot tells you in simple words what that part's code does. Your page doesn't change.
AI explanations can be wrong too. If something sounds strange, test it: change one small thing and look at the result. Seeing it with your own eyes is the best teacher.
Your free detective kit: browser DevTools
You met DevTools (short for developer tools) in Chapter 1. Every desktop browser has them built in. They show you the HTML, the CSS and the error messages of any page. Don't worry, you can't break anything: all changes disappear when you reload the page.
How to open DevTools
| Where | How |
|---|---|
| Windows / Linux | F12 or Ctrl + Shift + I (on some laptops: Fn + F12) |
| Mac | ⌘ Cmd + ⌥ Option + I |
| Chrome, Edge, Firefox | Right-click on the page → Inspect |
| Safari (Mac) | First, once: Safari → Settings → Advanced → turn on Show features for web developers. Then ⌘ Cmd + ⌥ Option + I. |
| Phone | Phone browsers don't have DevTools. Use a computer and the device toolbar (see below) to pretend to be a phone. |
The three parts you need today
This is a pretend DevTools window, drawn for this lesson. In this version of the film night page, the AI made a few mistakes, so you can see what problems look like. Click Elements, Console and the 📱 phone icon to explore it.
Elements shows your page's HTML as a tree. Click a line and that part lights up blue on the page. Below it, Styles shows the CSS for that element. A crossed-out line with ⚠️ means the browser didn't understand it. That's often a typo!
Console is where the browser writes messages. Red lines are errors. The grey text on the right, like film-night.html:39, tells you the file and the line number. Copy the red text into your bug report for AI.
The device toolbar (the 📱 phone-and-tablet icon, or Ctrl + Shift + M in Chrome and Edge, ⌘ Cmd + Shift + M on Mac) shows your page at phone size. Pick a phone at the top. Great for finding things that are too wide.
Try it now (1 minute) Press F12 on this page. Click Console: are there red lines? Then click the phone icon to see this page as a phone. Press F12 again to close DevTools.
Common AI mistakes (and how to spot them)
AI predicts code that looks right. It doesn't run it, and it also learned from lots of old code on the internet. These are the mistakes you will meet most often.
| Mistake | What you notice | How to fix it |
|---|---|---|
| 🏷️ Missing closing tag | The layout suddenly breaks: boxes inside boxes, or everything after one point is bold or a link. | Ask AI: “Check that every tag is closed.” Or paste your code into the free checker at validator.w3.org. |
| ✏️ Misspelled CSS property or color | backround, colour, #ff00zz: the style simply doesn't appear. No error message! |
DevTools → Elements → Styles shows the line crossed out with ⚠️. CSS uses American spelling: color, center. |
| 🔗 HTML and JavaScript use different ids | A button does nothing. The Console says Cannot read properties of null. |
Make id="…" and getElementById("…") exactly the same, letter by letter. |
| 🖼️ Wrong file or image path | A broken image icon, or styles are missing. The Console says 404 (Not Found). |
Check the folder, the spelling and capital letters: Photo.JPG ≠ photo.jpg. Upload the image together with your page. |
| 📱 Too wide on phones | The page scrolls sideways on a phone. Text is cut off. | Look for fixed widths like width: 1200px. Ask AI to use max-width: 100%. Test with the device toolbar. |
| 🎭 “Fake” features | A contact form says “Message sent!” but nothing arrives. A “login” with the password written in the code. | Ask: “Does this really send data anywhere?” A single HTML page can't send email by itself. Use a simple mailto: email link instead. |
| 🕰️ Outdated code | Old tags like <center>, <font> or <marquee>, or tables used for page layout. |
Ask for “modern HTML5 and CSS (flexbox or grid), no outdated tags”. |
| 🧩 Placeholders left in | “Lorem ipsum” text, links that go to #, example.com, a fake phone number. |
Search your code (Ctrl + F) for #, lorem and example, and replace them with real information. |
Find the bug: be the detective 🕵️
Each mini page below has one planted bug. Change the code in the dark editor: the preview updates while you type. Stuck? Press Hint. Still stuck? Press Show the fix. When you think you've got it, press Check my fix.
Bug 1 The button does nothing
Expected: click “Count me in!” and a green thank-you message appears.
Actual: nothing happens at all.
Open DevTools (F12) → Console and click the button again. The red error says Cannot read properties of null. “null” means “nothing”: JavaScript looked for an element and found nothing.
Now compare the button's id with the name inside getElementById(…), letter by letter.
The fix: make the two names the same.
The HTML calls the button rsvp-button, but JavaScript looked for rsvp-btn. Computers don't guess: one letter different means a totally different name. (Changing the HTML id to rsvp-btn works too. The two names just have to match.)
Bug 2 The card has no style
Expected: a dark purple card with white text.
Actual: plain dark text on a light background.
There are two small typos in the .card rule. CSS never shows an error: the browser silently skips every property it doesn't know.
Extra hint: CSS only understands American English spelling. (In real DevTools, the Styles panel would show both lines crossed out with ⚠️.)
The fix: correct the spelling of both properties.
backround is missing a “g”, and colour is British spelling. CSS only accepts background and color. Unknown properties are ignored without any warning on the page. That's why you have to look.
Bug 3 The boxes are inside each other
Expected: three boxes of the same size, side by side, and a line of text below them.
Actual: one big box with the two other boxes stuck inside it.
How to report a bug to AI
“It doesn't work, fix it” is the worst prompt in the world. The AI has to guess, and it often guesses wrong or rewrites your whole page. Give it five facts instead.
❌ Vague
My button doesn't work. Fix it.
Which button? What should happen? Any error? The AI can only guess.
✅ Clear
What I wanted: clicking "Count me in!" shows a thank-you message.
What happened: nothing happens when I click.
Error (Console): Cannot read properties of null (reading 'addEventListener')
The code: [the button line and the script]
What I tried: I reloaded the page.
Two golden rules Paste only the part of the code that matters (plus a few lines around it): huge pastes make answers worse. And ask for “only the lines I need to change”, so the AI doesn't rewrite the parts that already work.
Quick check
Your “Send” button does nothing, and the Console says: Cannot read properties of null (reading 'addEventListener'). What is the most likely cause?
You wrote colour: red; in your CSS. What happens?
The AI made a contact form that says “Thanks! Message sent.” Your site is a single HTML file on free hosting. What should you do?
Your turn (5 minutes)
- Open your own site in DigiBot Studio (or build a quick one).
- Turn on Click to edit, pick one part of your page and press Explain. Did you learn something new?
- Download your page (the ⬇ .html button above the preview) and open the file in your browser. Press F12 and look at the Console. Any red errors? Switch to phone view. Does anything stick out?