Created
October 13, 2016 17:01
-
-
Save martinjoiner/1395b0271a3f0271d305422f024455c4 to your computer and use it in GitHub Desktop.
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
| // rotateByN() moves a chunk of an array from the start to the end | |
| Array.prototype.rotateByN = function(n) { | |
| var arrReturn = [], | |
| i, | |
| iLimit = this.length; | |
| // Add everything above n | |
| for( i = n; i < iLimit; i++){ | |
| arrReturn.push( this[i] ); | |
| } | |
| // Add everything below n | |
| for( i = 0; i < n; i++){ | |
| arrReturn.push( this[i] ); | |
| } | |
| return arrReturn; | |
| }; | |
| // itemByLoopedIndex() will allow an item with an index higher than number of items to be requested | |
| Array.prototype.itemByLoopedIndex = function(n) { | |
| // If they are asking for a valid index item, just return it | |
| if( n < this.length ){ | |
| return this[n]; | |
| } | |
| var remainder = n % this.length; | |
| return this[remainder]; | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment