For wrapping some sum x + y inside of a range n, you can use (x + y) % n (and of course bounds-checking with if works too).
x + y
(x + y) % n
N.B. different languages treat negative mod differently. For JS, in the negative case you'd want something like ((x % n) + n) % n instead of plain %.
((x % n) + n) % n
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.