Skip to content

Instantly share code, notes, and snippets.

@tvler
Created April 16, 2018 23:22
Show Gist options
  • Save tvler/60bcec963a142c6d9360f6ef5c3b844a to your computer and use it in GitHub Desktop.
Save tvler/60bcec963a142c6d9360f6ef5c3b844a to your computer and use it in GitHub Desktop.
Get circular array index
const getCircularArrayIndex = (i, { length }) =>
(i % length + length) % length;
const testArray = new Array(5);
for (let i = -30; i < 31; i++) {
console.log(i, getCircularArrayIndex(i, testArray))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment