Created
April 6, 2015 16:26
-
-
Save kriscooke/574ac5f3c218c3b0c1ce to your computer and use it in GitHub Desktop.
Reversing an array (replace Array.prototype.reverse())
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.reverse = function() { | |
| // .splice(0) Empties out the entire array and returns the removed elements to arr. | |
| // (Uses deleteCount = Array.length) | |
| var arr = this.splice(0); | |
| // While arr.length is not falsy (arr.length > 0) | |
| // Pop off the end of arr, and push it back into the empty "this" array: | |
| while(arr.length) { | |
| this.push(arr.pop()); | |
| } | |
| // Return "this" array: | |
| return this; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment