Skip to content

Instantly share code, notes, and snippets.

@headquarters
Last active December 10, 2015 07:48
Show Gist options
  • Save headquarters/4403355 to your computer and use it in GitHub Desktop.
Save headquarters/4403355 to your computer and use it in GitHub Desktop.
A reminder to myself of how to handle array indexing in a "round robin" fashion, using the modulus operator.
//assume variables currentIndex, nextIndex, arrayLength exist
//moving "forward", positive incrementing
nextIndex = (currentIndex + 1) % arrayLength;
//moving "backward", negative incrementing
if(currentIndex == 0){
nextIndex = arrayLength - 1;
} else {
nextIndex = (currentIndex - 1) % arrayLength;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment