W2Week 2 of 4

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

This week uses: Variables · Assignment (=) · if/else · Comparison (==, ===, <, >) · for loop · while loop · Arrays [] · .length · ++, --

Covered in Lesson 1 & Lesson 2. If any feel unfamiliar, review before continuing.

Your progress0 / 15 complete

Estimated time remaining: 5-6 hours

The Format

  1. 1. Read the code and its description
  2. 2. Run it mentally (trace the state)
  3. 3. Find the bug — where does behavior diverge from intent?
  4. 4. Explain the bug — WHY is it wrong?
  5. 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.

1Exercise 2.1.1

The Missing Element

1 / 15

Intent: Print all elements of an array.

javascript
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
2Exercise 2.1.2

The Extra Iteration

2 / 15

Intent: Count down from 5 to 1.

javascript
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
3Exercise 2.1.3

The Fence Post

3 / 15

Intent: Build a string of numbers separated by commas: "1, 2, 3, 4, 5"

javascript
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.

4Exercise 2.2.1

Assignment Instead of Comparison

4 / 15

Intent: Check if a user is an admin.

javascript
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
5Exercise 2.2.2

Loose Equality Surprise

5 / 15

Intent: Check if input is the number zero.

javascript
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.

6Exercise 2.2.3

The Inverted Condition

6 / 15

Intent: Only allow positive numbers.

javascript
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.

7Exercise 2.3.1

AND vs OR

7 / 15

Intent: Reject passwords shorter than 8 characters OR longer than 20.

javascript
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
8Exercise 2.3.2

Missing Else Branch

8 / 15

Intent: Categorize age into child, teen, or adult.

javascript
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
9Exercise 2.3.3

The Empty Check That Doesn't

9 / 15

Intent: Only greet users who have entered a name.

javascript
1let name = " "; // Just spaces
2
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.

10Exercise 2.4.1

The Vanishing Variable

10 / 15

Intent: Calculate a total and use it outside the loop.

javascript
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
11Exercise 2.4.2

The Clobbered Global

11 / 15

Intent: Track the largest number found.

javascript
1let max = 0;
2
3function findMax(numbers) {
4 let max = numbers[0]; // Oops
5
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
12Exercise 2.4.3

The Loop Variable Leak

12 / 15

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.

javascript
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.'

13Exercise 2.5.1

The Infinite Loop

13 / 15

Intent: Print numbers until we hit 10.

javascript
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
14Exercise 2.5.2

The Early Exit

14 / 15

Intent: Find if a number exists in an array.

javascript
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
15Exercise 2.5.3

The Wrong Update

15 / 15

Intent: Sum all numbers from 1 to n.

javascript
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.