Created
March 4, 2014 06:53
-
-
Save acoard/9341531 to your computer and use it in GitHub Desktop.
Adds a .randomElement() method to lists by extending Object.prototype
This file contains 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
//This little snippet does the bad practice of overloading default JavaScript objects. I know it's bad taste, sue me. | |
//It only works on arrays. | |
//['a', 'b', 'c'].randomElement() | |
Object.prototype.randomElement = function(){ | |
var randomIndex = Math.floor(Math.random() * this.length); | |
return randomString = this[randomIndex]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment