Skip to content

Instantly share code, notes, and snippets.

@kriscooke
Created April 6, 2015 16:26
Show Gist options
  • Select an option

  • Save kriscooke/574ac5f3c218c3b0c1ce to your computer and use it in GitHub Desktop.

Select an option

Save kriscooke/574ac5f3c218c3b0c1ce to your computer and use it in GitHub Desktop.
Reversing an array (replace Array.prototype.reverse())
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