Basic Web Development

Lesson 10 of 10

Bug Hunt

A page with four features and four bugs. Your job is to find them and fix them.

What you will learn

  • How to debug
  • The F12 console
  • console.log
  • = and ===
  • Infinite loops
  • Positions past the end
  • Printing instead of returning

Before you start

Every lesson before this one. The four bugs are the four mistakes you have already met.

What to do

  1. Try each feature in the preview above. Watch how each one goes wrong.
  2. Open bugs.js and the console. Press F12 to open the console.
  3. Find each bug, fix it, reload, and check that the feature now works.
  4. There are hints at the bottom of bugs.js. They start small and get more direct.

The rules of the hunt

  • Each feature has exactly one bug.
  • The comments are all true. They describe what the code is supposed to do. When a comment and its code disagree, the code is wrong.
  • The safety brake in Hunt 2 is not a bug. It stops a runaway loop from freezing the tab. Leave it in place.

The three debugging moves

Do not read the code line by line hoping to spot the mistake. Use a method.

MoveWhat you doQuestion to ask
Run itUse the feature and watch exactly what goes wrong.What did I expect, and what happened instead?
Read itRead the few lines that produce that symptom.Does the code do what its comment says?
Probe itAdd console.log to print a value at a chosen moment.Is this value what I think it is?

Then fix one thing, reload, and check. Change one thing at a time. If you change three things and it works, you do not know which one fixed it.

Try these changes

Make these in the files beside this guide. None of them need anything the lesson has not covered.

  • Write a fifth broken feature for someone else to hunt. Writing a good bug means understanding it well.
  • For each bug, write the single `console.log` line that finds it fastest.
  • Start from a fresh copy and fix all four in under 10 minutes.
Teacher's versionHow to run this lesson, what to check, and any answers. Students do not need this.

How to teach it

Do not point at lines. Teach the loop: symptom, then guess, then probe, then fix, then check.

  1. Do Hunt 1 together

    Symptom: every password is accepted. Guess: the if is always true. Probe: read the condition one character at a time. Fix it, reload, and test both a right answer and a wrong answer.

  2. Let them do Hunts 2 to 4 alone

    Only step in with questions. "What does the console say?" "Which way is count moving?" "How many finishers are there, and what is the last position?"

  3. Debrief every fix

    Ask the student to name the kind of bug in one sentence. That vocabulary is exactly what sections B and C of the readiness test reward.

Answer key

Do not open this in front of the student.

HuntThe bugThe fixKind of bug
1. Password Gateif (guess = "acorn") sets the value instead of comparing it. The condition is always true.if (guess === "acorn")Assignment instead of comparison
2. Launch Countdowncount = count + 1 moves the count away from 0, so count > 0 never becomes false.count = count - 1Infinite loop
3. Medal Ceremonyfinishers[5] is used. Five items means the valid positions are 0 to 4, so position 5 is undefined.finishers[4], or better finishers[finishers.length - 1]Position past the end
4. Tip CalculatoraddTip() prints the answer but returns nothing, so total receives undefined.return bill * 1.2;Printing instead of returning