In the last lesson, you paused the practice copy of Alex’s progress count and moved through it with Step over, Step into, Step out, and Resume. That code already worked, so the buttons were the only new thing. A real bug is different. The debugger shows you every value at a pause, but it doesn’t tell you where to pause or what a wrong value means.
In this lesson, you’ll find a bug with a four-step method that turns the buttons into a search. You’ll learn to treat a missing pause at a breakpoint as a clue, and you’ll decide when a labeled log is still the quicker tool. Start with the method and a count that’s wrong.
Guess Where, Then Pause There
A breakpoint helps only if it sits near the bug. Stepping through every line from the top works in a toy file, but in the packing list, a count passes through several functions and files before it reaches the page. So the method starts before you open the Sources tab:
- Guess where: Use the expected and actual values and the clue in the wrong one to pick the line you suspect first.
- Pause there: Set a breakpoint on that line, or a little before it, then make that code run again. Reload for code that runs at load, or repeat the click, checkbox selection, or form submission that runs a handler.
- Inspect: Read the values in the Scope pane, and step to watch what runs next.
- Fix and rerun: Remove the breakpoint, fix the line, rerun the code the same way, and check your test inputs again.
The first step is what you practiced in Read the Clues in Wrong Output, where you read clues and worked out expected values by hand. What changes is the second step. Instead of adding a log line and reloading, you pause on the line you suspect and read everything at once.
To try it, you’ll use the same progress-bug folder as last time. Its data file still holds five of Beach Week’s items, with only the swimsuit and the passport packed:
items.js
export const items = [
{ name: "Swimsuit", category: "Clothes", packed: true },
{ name: "Sun hat", category: "Clothes", packed: false },
{ name: "Sunscreen", category: "Toiletries", packed: false },
{ name: "Toothbrush", category: "Toiletries", packed: false },
{ name: "Passport", category: "Documents", packed: true },
];
This time, the count lives in a function, countPacked(), which counts one category’s packed items. That’s a tidier shape than last lesson’s counts object, but this version has a slip in it. Replace the contents of progress.js with the following code, blank lines included, so your line numbers match the ones below:
progress.js
import { items } from "./items.js";
const categories = ["Clothes", "Toiletries", "Documents"];
function countPacked(category) {
let packed = 0;
for (const item of items) {
if (item.packed && item.category === category) {
packed += 1;
}
return packed;
}
}
function progressLine(category) {
const total = items.filter(
(item) => item.category === category,
).length;
const packed = countPacked(category);
return `${category}: ${packed} of ${total} packed`;
}
for (const category of categories) {
console.log(progressLine(category));
}
The page that loads it hasn’t changed since you made the folder: