Skip to content

Instantly share code, notes, and snippets.

@pbzona
Created September 22, 2020 20:06
Show Gist options
  • Save pbzona/9b829ae73d8f292c91c4ddc08ebab607 to your computer and use it in GitHub Desktop.
Save pbzona/9b829ae73d8f292c91c4ddc08ebab607 to your computer and use it in GitHub Desktop.
Wrapping around an array
const magicNumber = 3;
const dataArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
let index = 0;
while (true) {
// This is where you get the current item
// I'm just printing it but you can do anything
console.log(dataArray[index]);
// Perform some check on whether to keep going
if (dataArray[index] === 3) {
break;
}
// Increase the `current` value by the magic number
// Use modulo to wrap around
index = (index + magicNumber) % dataArray.length
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment