Lesson 10: Debug with AI
| Course home | Previous: Lesson 9 | Next: Lesson 11 |
Time: about 60 minutes.
Goals
By the end of this lesson you will be able to:
- Use browser DevTools: Console, Elements, Application (Storage) and Sources.
- Read an error message and a stack trace.
- Follow a reliable debugging loop: reproduce, isolate, explain, fix, prevent.
- Write a bug report an agent can act on.
- Know when to stop asking the agent and revert instead.
Concepts
DevTools tour
Open DevTools with F12, or Cmd+Option+I on macOS. The Chrome DevTools docs and MDN’s JavaScript debugging guide cover everything; you need four panels:
| Panel | Use it to |
|---|---|
| Console | See errors (red) and warnings; run quick JavaScript |
| Elements (Inspector in Firefox) | See the live HTML and CSS; check what is actually on the page |
| Application (Storage in Firefox) | View and edit localStorage |
| Sources (Debugger in Firefox) | Pause code on a line with a breakpoint and inspect values |
Reading an error
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
at app.js:121:6
- Type:
TypeError, a value was the wrong kind. - Message: something was
null, and the code tried to use.addEventListeneron it. - Location:
app.js, line 121, column 6.
Translation: document.querySelector(...) found nothing (returned null), probably because the id in JavaScript does not match the id in HTML.
Common beginner errors:
| Error | Usual meaning |
|---|---|
Cannot read properties of null / undefined |
An element or value you expected does not exist |
X is not a function |
A typo, a missing import, or the thing is not what you think |
X is not defined |
A variable or function was never declared or imported |
Unexpected token |
A syntax mistake: a missing bracket, comma or quote |
Failed to load module script / CORS errors on file:// |
You opened the HTML directly instead of through a local server |
The requested module does not provide an export named X |
Import name does not match the export |
The debugging loop
- Reproduce: find exact steps that trigger the bug every time.
- Isolate: narrow it down. Which file? Which function? Which input?
- Explain: state the cause in one sentence before changing anything.
- Fix: the smallest change that addresses the cause.
- Prevent: add a test that would have caught it.
Agents are good at steps 2 to 4 if you give them step 1. Without exact steps and error text, they guess.
A bug report agents can act on
Bug: <one-line summary>
Steps:
1. ...
2. ...
Expected: ...
Actual: ...
Console: <exact error text, copied, not paraphrased>
Already tried: ...
Please find the cause and explain it before changing anything.
When to stop
If the agent has tried two fixes and the bug is still there:
- Stop. Do not say “try again”.
git restore .to get back to the last working commit.- Start a fresh session with the full bug report and “explain the cause first”.
- If that fails, isolate it yourself with a breakpoint or a smaller test case.
Repeated retries in a long session tend to pile changes on top of a misunderstanding.
Walkthrough
You will plant bugs in your own app on purpose and then debug them. Commit your work first so you can always get back:
git status # should be clean
Bug 1: the id mismatch
In index.html, change id="habit-list" to id="habits-list". Reload the app with the Console open.
- Reproduce: just loading the page. Read the red error.
- Isolate: click the
app.js:NNlink in the error. DevTools opens the line. - Explain: in one sentence, what is wrong? Write it down before asking the agent.
- Now ask the agent with a proper report:
Bug: the page shows no habits and the form does nothing.
Steps: 1) start the server 2) open http://localhost:8000
Expected: the app works as before.
Actual: nothing happens when I press Add.
Console: [paste the exact error and the line number]
Find the cause and explain it before changing anything.
Compare its explanation with yours. Then git restore index.html to undo your planted bug.
Bug 2: the logic bug that throws no error
In habits.js, in currentStreak, change addDays(day, -1) inside the loop to addDays(day, 1). Save and reload.
No error appears. The streak just looks wrong. This is the common kind of bug: silent.
- Run
node --test. Which tests fail? The failure output tells you expected vs actual. - Use a breakpoint: in DevTools Sources, open
habits.js, click the line number inside thewhileloop, tick a habit. Execution pauses. Hover overdayandstreak. Press the “step over” button a few times and watchdaymove the wrong way. - Ask the agent, giving it the failing test output:
node --test shows this failure: [paste]. Find the cause in habits.js and
explain it in one sentence. Do not change the test.
Restore: git restore habits.js.
Bug 3: the data bug
In DevTools Application, Local storage, change your saved value so that one habit’s done list contains "2026-13-45". Reload.
- If your defensive loading from Lesson 7 works, the bad date is dropped and the app is fine. Check with the Elements panel that the seven-day strip renders.
- If the app breaks, you have found a real bug. Write a bug report and have the agent fix it, then add a test in
tests/storage.test.jsfor this exact input.
Bug 4: the “works on my machine” bug
Open the app by double-clicking index.html (a file:// URL) instead of using the server. The app does not start. The Console shows a module or CORS error.
Ask the agent:
When I open index.html by double-clicking it, the console shows: [paste].
When I use python3 -m http.server it works. Explain why in plain English.
Do not change any files.
Good answer: browsers apply security rules to JavaScript modules and refuse to load them from file:// pages; a local server serves them over http:// so they load normally.
Bug 5: a real one, if you have it
By now you have probably hit a real bug in your own version. Write a full bug report for it using the template, run it through the loop, and finish with a test that would have caught it. Commit the fix and the test together:
git commit -am "Fix <bug> and add regression test"
What good output looks like
- For each bug you can state the cause in one sentence.
- Your prompts include exact steps, expected, actual and copied error text.
- The agent explained before it edited.
- Every real bug fixed comes with a new test.
- Planted bugs were removed with
git restore, leaving a clean history.
Checklist
- I can open the Console, Elements, Application and Sources panels.
- I can read an error’s type, message and location.
- I set a breakpoint and stepped through code.
- I wrote at least two full bug reports.
- I fixed one real bug with a regression test.
- I know my “stop and revert” rule.
Mini-quiz
- What does
Cannot read properties of null (reading 'addEventListener')usually mean in a small web app? - What are the five steps of the debugging loop?
- Why paste the exact error text rather than describing it?
- A bug produces no error at all. What two tools from this course help most?
- The agent has tried three fixes and the bug remains. What do you do?
Answers
1. A `querySelector` (or similar) returned `null`: the element was not found, often because of an id mismatch between HTML and JavaScript, or the script ran before the element existed. 2. Reproduce, isolate, explain, fix, prevent. 3. The exact text includes the error type, the precise message and the file and line, which point straight to the cause. A paraphrase loses those details. 4. Automated tests (to show expected versus actual) and breakpoints in DevTools (to watch values change step by step). 5. Stop, revert to the last working commit with Git, start a fresh session with a full bug report asking for the cause first, and isolate it yourself if needed.Assignment
- Ask the agent to plant one subtle bug in a copy of your project without telling you where (
git stashor a new branch makes this easy to undo; ask the agent how). Find it using only DevTools and tests. - Write a one-page “Debugging cheatsheet” for your future self in your project’s README: your favourite error explanations and your bug report template.
Next: Lesson 11: Stay safe