Created
March 22, 2012 01:05
-
-
Save holtbp/2154915 to your computer and use it in GitHub Desktop.
Duplicate Array without Assignment
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
// 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