Today I Learned

Implement moving array pointer index.

When you have an array with index pointing to eg. selected item and this index should move.

Solutions I often saw take multiple lines and if statements to prevent index from pointing out of an array.

However there is a one liner way.

// vector is 1 or -1 depending on user interaction

return (currentIndex + arrayLength + vector) % arrayLength;

To better illustrate I have made code sandbox example.