Skip to content

Instantly share code, notes, and snippets.

View fengyuanchen's full-sized avatar

Fengyuan Chen fengyuanchen

View GitHub Profile
@fengyuanchen
fengyuanchen / String.prototype.capitalize.js
Created July 10, 2015 05:52
Forces the first letter of each word to be converted to uppercase (the other characters will not be changed)
String.prototype.capitalize = function () {
return this.replace(/\w+/g, function (word){
return word.charAt(0).toUpperCase() + word.substr(1);
});
};
@fengyuanchen
fengyuanchen / Array.prototype.randomSort.js
Created July 10, 2015 05:48
Sorts the elements of an array randomly
Array.prototype.randomSort = function () {
var i = this.length;
while (i--) {
this.splice(Math.floor(this.length * Math.random()), 0, this.pop());
}
return this;
};
@fengyuanchen
fengyuanchen / Array.prototype.random.js
Created July 10, 2015 05:45
Get a random element from an array
Array.prototype.random = function () {
return this[Math.floor(this.length * Math.random())];
};