Debugging & Unit Testing Lab
Sharpen your debugging instincts by hunting for bugs in code snippets, designing test cases that expose hidden defects, and classifying error types from symptoms and messages. Track your accuracy across all three activities.
Guided Experiment: Systematic Bug Hunting
Which types of code lines are most commonly buggy? Do certain patterns (loops, conditionals, array access) contain bugs more often than others?
Write your hypothesis in the Lab Report panel, then click Next.
Controls
Click the line you think contains the bug, then reveal the answer.
1function sumArray(arr) {2 let sum = 0;3 for (let i = 0; i <= arr.length; i++) {4 sum += arr[i];5 }6 return sum;7}Data Table
(0 rows)| # | Activity | Item | Result | Score | Notes |
|---|
Reference Guide
Common Bug Types
Most bugs fall into a few recognizable categories. Learning to spot these patterns speeds up debugging.
- Off-by-one - Loop runs one too many or too few times (using <= instead of <, or starting at 1 instead of 0)
- Null reference - Calling a method on null or undefined without checking first
- Logic error - Code runs without crashing but produces wrong results (wrong operator, broken swap, missing condition)
- Infinite loop - Loop variable never changes, or termination condition is never met
Test Design Principles
Good tests are targeted and systematic. Cover these areas to maximize bug detection.
- Boundary values - Test at the edges: empty input, single element, maximum size, zero, negative numbers
- Equivalence partitioning - Group inputs by expected behavior and test one from each group
- Edge cases - Special values like null, empty strings, duplicates, already-sorted arrays
- Expected vs actual - Always verify both the return value and any side effects
Debugging Strategy
Follow a systematic approach instead of guessing. Each step narrows the search.
- Reproduce - Find the specific input that triggers the bug
- Isolate - Narrow down which section of code is responsible
- Identify - Understand exactly why the code behaves incorrectly
- Fix - Make the smallest change that corrects the behavior
- Verify - Confirm the fix works and does not break other cases
Error Categories
Errors fall into distinct categories based on when and how they manifest.
- Syntax - Code cannot be parsed (missing brackets, semicolons, typos)
- Runtime - Code crashes during execution (stack overflow, invalid operations)
- Logic - Code runs but produces wrong output (no error message)
- Type - Wrong data type is used (string where number expected)
- Boundary - Code fails at extreme or limit values (overflow, empty input)