Skip to content

Instantly share code, notes, and snippets.

@holtbp
Created March 22, 2012 01:05
Show Gist options
  • Save holtbp/2154915 to your computer and use it in GitHub Desktop.
Save holtbp/2154915 to your computer and use it in GitHub Desktop.
Duplicate Array without Assignment
// Given an array, var arr = [1,2,3,4,5] how do you implement a function duplicator such that
// arr.duplicator() == [1,2,3,4,5,1,2,3,4,5]?
// Duplicate array elements by modifying array instead of assigning the returned array to a new var.
Array.prototype.duplicator = function(obj) {
this.map(duplicate);
};
function duplicate(element, index, array){
return array.push(element);
}
var arr = [1,2,3,4,5];
arr.duplicator();
console.log(arr);​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment