Workshop › Rhythm Lanes › Step 15 of 35
Which note does a press hit? The closest one in that lane that is not hit yet. We search the list and keep the best
candidate so far in closest.
```js
function press(lane) {
lit[lane] = 8
let closest = null
for (const n of notes) {
if (n.hit || n.lane !== lane) continue
if (!closest || Math.abs(n.time - frame) < Math.abs(closest.time - frame)) closest = n
}
if (!closest) return
closest.hit = true
}
```closest starts as null, "nothing yet".!== means "not equal").Math.abs drops the sign: how far a note's time is from now, early or late.return; otherwise the closest note is hit.In press, under lit[lane] = 8, write the new lines.
Skills: Loops
← Show the keys Close enough in time →