Modulo and repeating patterns
% returns the remainder after division. Feed it a counter and you get a repeating cycle for free:
for (let i = 0; i < 8; i++) {
console.log(i % 3); // 0 1 2 0 1 2 0 1
}
That repeating 0 1 2 is the same building block the game later uses for clock hands, blinking lights, and cycling animation frames. The % of a large number by a small one is always "where you are in the cycle."
A decimal example worth remembering: 10 / 4 is 2.5, but 10 / 2 is 5. JavaScript keeps numbers as one kind of box, but watch the division result.
Why clamping protects your game
Without clamps, values run away. Imagine the game's speed: it ramps up the longer you survive, but a player can't react to something moving at 10,000 px/s. The cap is what makes it a game and not a blur.
let speed = 340;
speed += 4000; // a frame-rate spike, or a bug
speed = clamp(speed, 340, 720); // -> 720
The finished game does exactly this with Math.min(CONFIG.MAX_SPEED, ...). Clamping is not cheating, it's the boundary that keeps difficulty hard but winnable. We'll return to this idea of caps as fairness in the Advanced tier.