Workshop › OutRun-style Racer › Step 30 of 44
"How far ahead is that car?" is harder on a looping track: a car at 100 may be just ahead of us at 159 900. ahead(a, b)
is the distance going forwards from a to b, always between 0 and the track length.
```js
// Distance from a to b going forwards along the looping track.
const ahead = (a, b) => (((b - a) % trackLength) + trackLength) % trackLength
```(b - a) % trackLength can still be negative in JavaScript: -30 % 100 is -30.trackLength makes it positive, and the second % brings a positive result back under trackLength.Under the segmentAt line write the comment and ahead.
Skills: Types and values
← Other cars Cars that shrink with distance →