Find a course

Module 3: Functions and DebuggingLesson 7 of 79 min

Debugging: Be a Bug Detective

Every coder makes mistakes. Learn the bug detective method, spot common bugs like typos and wrong symbols, and fix broken code yourself.

Step 1 of 12Learn

Everyone makes bugs

Here is a secret: even expert programmers write code with mistakes every single day. A mistake in code is called a bug, and finding and fixing it is called debugging.

Bugs are not a sign that you are bad at coding. They are a normal part of it. What matters is having a good way to hunt them down.

Step 2 of 12Diagram

Write, run, fix, repeat

Write, run, fix, repeatCycle diagram: Write code → Run it → Bug! Find it → Fix it.TotallynormalWrite codeRun itBug! Find itFix it
Write code
Run it
Bug! Find it
Fix it

Step 3 of 12Tap to reveal

The bug detective method

  1. 1. Notice

    What did you expect to happen? What happened instead? Say it out loud.

  2. 2. Find

    Go through the code line by line. Pretend you are the computer and follow each step exactly.

  3. 3. Fix

    Change one thing at a time, so you know which change fixed it.

  4. 4. Test

    Run the code again, and try a few different inputs to be sure.

Step 4 of 12Practice · Put in order

Put the bug detective steps in order.

Drag the items, or use the arrow buttons, to put them in order.

  1. Notice what went wrong
  2. Fix one thing
  3. Find the line causing it
  4. Test it again

Step 5 of 12Learn

Common bugs

Most beginner bugs are one of these:

  • Typos: writing scroe instead of score. The computer thinks it is a different name.
  • Missing pieces: forgetting a quote mark, a bracket } or a closing ).
  • Wrong symbol: using + when you meant *, or > when you meant <.
  • Wrong order: showing the score before adding the points.
  • Wrong number: a loop that repeats 9 times when you wanted 10.

Step 6 of 12Diagram

Five common bugs

Five common bugsParts diagram: Common bugs, Typo scroe, Missing } or ", Wrong symbol + *, Wrong order, Wrong number 9/10.Common bugsTypo scroeMissing } or "Wrong symbol +*Wrong orderWrong number9/10
Common bugs
Typo scroe
Missing } or "
Wrong symbol + *
Wrong order
Wrong number 9/10

Step 7 of 12Practice · Spot the mistake

This code should add 5 to the score, but it crashes. Tap the bug.

Step 8 of 12Practice · Code challenge

This function should return a number doubled, but double(4) gives 6 instead of 8. Find the bug and fix it.

Runs in a sandbox in your browser.
  • double(4)
  • double(10)
  • double(1)

Step 9 of 12Note

Step 10 of 12Practice · Sort into groups

What kind of bug is each one?

Tap an item, then tap its group (or drag it there).

Step 11 of 12Practice · What would you do?

What would you do?

Your loop should draw 10 stars but only draws 9. You have changed five different things at once and now nothing works. What should you do?

Step 12 of 12Practice · True or false

Good programmers almost never have bugs in their code.