Created
January 5, 2016 11:27
-
-
Save hellerve/be15db7d66edcd147ff5 to your computer and use it in GitHub Desktop.
A next() and previous() method for arrays with builtin wraparound
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
Array.prototype.next = function(i) { | |
return this[++i % this.length]; | |
} | |
Array.prototype.previous = function(i) { | |
return i === 0 ? this[this.length - 1] : this[--i % this.length]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
coo.