Created
September 22, 2020 20:06
-
-
Save pbzona/9b829ae73d8f292c91c4ddc08ebab607 to your computer and use it in GitHub Desktop.
Wrapping around an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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