Skip to content

Instantly share code, notes, and snippets.

@joshuacerbito
Created November 20, 2018 04:15
Show Gist options
  • Save joshuacerbito/2583ea066d48d43616d8ae3ae468dafb to your computer and use it in GitHub Desktop.
Save joshuacerbito/2583ea066d48d43616d8ae3ae468dafb to your computer and use it in GitHub Desktop.
Optimal logic for moving foward and backward through your array
/*
* 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