Here is a secret professional programmers know: nobody writes code that works the first time. Bugs — mistakes that make the code do the wrong thing — are completely normal. What separates a good coder from a frustrated one isn't avoiding bugs, it's being a calm detective who can track them down. This session is all about that detective work.
There are two kinds of bug:
This preview works: the ball bounces off all four walls and leaves a glowing trail. Watch one side bounce carefully so you know exactly what the starter code is supposed to do.
The full starter code below contains one silent bug. Copy it into the editor and run it. The ball will escape through the left or right wall. Use the five-step method to find the cause before opening Case 1.
console.log(x, vx) to see what a variable actually is. Bugs love hiding in "I assumed it was 5, but it was 0."// until the bug disappears. The last line you removed is near the culprit.== (compare) or = (set)? Is the array index off by one?Case 1 is the bug in the full starter code. Cases 2 and 3 are two fresh bugs to solve after that. Find each one before opening the answer.
if (x < r || x > width - r) vx * -1;vx * -1 just calculates −vx and throws it away — it never stores it back. It must be vx *= -1 (or vx = vx * -1). A silent bug: no error, wrong behaviour.function draw() { background(13, 17, 23, 40); x += vx; bally += vy; circle(x, y, r * 2);}ReferenceError. Where does step 1 (read the error) point you?y, but the code says bally — a typo. The error even names it: "bally is not defined." Fix: y += vy;. Step 4 (check spelling) catches this.let score = 0;function mousePressed() { score = score + 1; score += 1; console.log(score);}score = score + 1 and score += 1 do the same job. Delete one line. (These "add twice by accident" bugs are super common.)= vs ==. One = sets a value; two == compares. Writing if (score = 10) secretly sets the score to 10 and is always treated as true. Always double-check: am I asking a question (==) or giving a command (=)?
console.log(x, y) inside draw(). Open the console and watch the numbers change. (This is your most useful debugging tool.)vy *= -1 to vy *= 1. Predict what happens before you run it, then run it. Were you right? Now fix it.- r from x > width - r. Watch the ball sink half-way into the wall before bouncing. Explain why using the radius.vx to vX in just one place). Read the red error, then use it to find and fix your own bug.