Code Comments Lesson

Practice Scene

Code comments are notes written for people reading the code. A useful comment explains why something is happening, clarifies a tricky decision, or records an assumption that is not obvious from the code alone.

Code Runner Challenge

Code Comments Lesson

View IPYNB Source
%%js

// CODE_RUNNER: Code Comments Lesson

function clampPlayerToScreen(player, canvas) {
  // Keep the player visible after movement has already changed x.
  player.x = Math.max(0, Math.min(player.x, canvas.width - player.width));
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Why This Scene Works

Comments should not repeat simple code in plain English. A comment like “add one to score” does not help much when the next line already says score = score + 1. The best comments explain meaning, not syntax.

The comment explains why the line exists. The code itself shows the math, but the comment explains the purpose of the boundary check.