Bug Hunting
Find bugs in broken programs. Explain WHY they're wrong.
The skill: Epistemic humility + Computational empathy. Know that code can fail. Think like the machine to find where.
Time: 5-6 hours total
Estimated time remaining: 5-6 hours
The Format
- 1. Read the code and its description
- 2. Run it mentally (trace the state)
- 3. Find the bug — where does behavior diverge from intent?
- 4. Explain the bug — WHY is it wrong?
- 5. Specify the fix — describe what should change (don't write code)
The explanation is the artifact. It externalizes your understanding of why code fails.
Exercise 2.1: Off-by-One Errors
Humans count from 1. Machines count from 0. This isn't arbitrary — it's how memory works. These bugs reveal the gap between human intuition and machine structure.
The Missing Element
Intent: Print all elements of an array.
1let colors = ["red", "green", "blue"];2 3for (let i = 1; i <= colors.length; i++) {4 console.log(colors[i]);5}Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
green blue undefined
What it should output:
red green blue
The Extra Iteration
Intent: Count down from 5 to 1.
1let count = 5;2 3while (count >= 0) {4 console.log(count);5 count--;6}Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
5 4 3 2 1 0
What it should output:
5 4 3 2 1
The Fence Post
Intent: Build a string of numbers separated by commas: "1, 2, 3, 4, 5"
1let result = "";2 3for (let i = 1; i <= 5; i++) {4 result = result + i + ", ";5}6 7console.log(result);Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
1, 2, 3, 4, 5,
What it should output:
1, 2, 3, 4, 5
Exercise 2.2: Comparison Operator Mistakes
Assignment and comparison look almost identical but mean entirely different things. This is about the difference between describing the world and changing it — a distinction fundamental to all computation.
Assignment Instead of Comparison
Intent: Check if a user is an admin.
1let role = "user";2 3if (role = "admin") {4 console.log("Access granted");5} else {6 console.log("Access denied");7}Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
Access granted
What it should output:
Access denied
Loose Equality Surprise
Intent: Check if input is the number zero.
1let input = "0";2 3if (input == 0) {4 console.log("Input is zero");5} else {6 console.log("Input is not zero");7}Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
Input is zero
What it should output:
Input is not zero
Epistemic humility moment: JavaScript's == rules are genuinely confusing. This isn't a personal failing — it's a known source of bugs. Most professional codebases ban == entirely.
The Inverted Condition
Intent: Only allow positive numbers.
1let number = -5;2 3if (number < 0) {4 console.log("Valid number");5} else {6 console.log("Number must be positive");7}Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
Valid number
What it should output:
Number must be positive
Exercise 2.3: Logic Errors in Conditionals
Conditionals are how minds make decisions. AND vs OR, truthy vs true — these bugs reveal that natural language is ambiguous, but computation requires precision. The machine forces us to say exactly what we mean.
AND vs OR
Intent: Reject passwords shorter than 8 characters OR longer than 20.
1let password = "abc";2 3if (password.length < 8 && password.length > 20) {4 console.log("Invalid password length");5} else {6 console.log("Password length OK");7}Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
Password length OK
What it should output:
Invalid password length
Missing Else Branch
Intent: Categorize age into child, teen, or adult.
1let age = 10;2let category;3 4if (age < 13) {5 category = "child";6}7if (age < 20) {8 category = "teen";9}10if (age >= 20) {11 category = "adult";12}13 14console.log(category);Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
teen
What it should output:
child
The Empty Check That Doesn't
Intent: Only greet users who have entered a name.
1let name = " "; // Just spaces2 3if (name) {4 console.log("Hello, " + name + "!");5} else {6 console.log("Please enter your name");7}Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
Hello, !
What it should output:
Please enter your name
Exercise 2.4: Variable Scope Issues
Scope is how minds partition themselves. A variable isn't just a name — it's a region where information lives and dies. Understanding scope is understanding how computation manages what it knows and when it forgets.
The Vanishing Variable
Intent: Calculate a total and use it outside the loop.
1for (let i = 0; i < 3; i++) {2 let total = 0;3 total = total + i;4}5 6console.log(total);Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
ReferenceError: total is not defined
What it should output:
3
The Clobbered Global
Intent: Track the largest number found.
1let max = 0;2 3function findMax(numbers) {4 let max = numbers[0]; // Oops5 6 for (let i = 1; i < numbers.length; i++) {7 if (numbers[i] > max) {8 max = numbers[i];9 }10 }11}12 13findMax([3, 7, 2, 9, 4]);14console.log(max);Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
0
What it should output:
9
The Loop Variable Leak
Intent: Create three buttons that log their index.
New syntax: setTimeout(fn, ms) — calls the function fn after ms milliseconds. The function runs later, not immediately.
1// Old-style JavaScript (var instead of let)2for (var i = 0; i < 3; i++) {3 setTimeout(function() {4 console.log("Button " + i);5 }, 100);6}Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
Button 3 Button 3 Button 3
What it should output:
Button 0 Button 1 Button 2
Historical note: This bug caused so much pain that JavaScript added let and const in 2015. If you see var in code, treat it as a warning sign.
Exercise 2.5: Loop Termination Bugs
Loops are how computation handles time — doing something until a condition changes. These bugs reveal the gap between our intuition about 'until' and the machine's literal interpretation. The machine has no sense of 'eventually.' It only knows 'now' and 'next.'
The Infinite Loop
Intent: Print numbers until we hit 10.
1let n = 1;2 3while (n !== 10) {4 console.log(n);5 n = n + 2;6}Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
1 3 5 7 9 11 13 ... (forever)
What it should output:
1 3 5 7 9
The Early Exit
Intent: Find if a number exists in an array.
1function contains(arr, target) {2 for (let i = 0; i < arr.length; i++) {3 if (arr[i] === target) {4 return true;5 } else {6 return false;7 }8 }9}10 11console.log(contains([1, 2, 3], 3));Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
false
What it should output:
true
The Wrong Update
Intent: Sum all numbers from 1 to n.
1function sumTo(n) {2 let total = 0;3 let i = 1;4 5 while (i <= n) {6 total = total + i;7 }8 9 return total;10}11 12console.log(sumTo(5));Find the bug, explain why it's wrong, then specify what the fix should accomplish.
What it outputs:
(infinite loop - program hangs)
What it should output:
15
Computational empathy moment: The machine does exactly what you say. If you don't say "increment i", it won't. The machine has no intuition about what you meant.