Created
November 20, 2018 04:15
-
-
Save joshuacerbito/2583ea066d48d43616d8ae3ae468dafb to your computer and use it in GitHub Desktop.
Optimal logic for moving foward and backward through your 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
/* | |
* Optimal logic to move through your array forward and backward | |
*/ | |
const arr = ['a', 'b', 'c', 'd', 'e']; | |
let currentIndex = 0; | |
function moveForward(current, array) { | |
return (current + 1) % array.length; | |
} | |
function moveBackward(current, array) { | |
return (current - 1 + array.length) % array.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment