Skip to content

Instantly share code, notes, and snippets.

@zgulde
Last active January 1, 2016 09:00
Show Gist options
  • Save zgulde/67111b6d77c2797fa5f8 to your computer and use it in GitHub Desktop.
Save zgulde/67111b6d77c2797fa5f8 to your computer and use it in GitHub Desktop.
Number.prototype.times = function(cb){
for (var i = 0; i < this; i++) {
cb();
}
}
Number.prototype.to = function(n, step){
var array = [];
step = (typeof step != 'undefined') ? step : 1;
if (n > this){
for (var i = 0; i < n; i += step) {
array.push(this + i);
}
} else if (n < this) {
for(var i = 0; i < this; i += step){
array.push(this - i);
}
} else {
return [n];
}
return array;
}
Number.prototype.isOdd = function(){
return (this % 2 == 1);
}
Number.prototype.isEven = function(){
return (this % 2 == 0);
}
Array.prototype.sample = function(){
return this[Math.floor(Math.random() * this.length)];
}
Array.prototype.first = function(n){
n = (typeof n != 'undefined') ? n : 1;
return this.slice(0, n);
}
Array.prototype.last = function(n){
n = (typeof n != 'undefined') ? n - 1 : 0;
return this.slice(this.length - 1 - n, this.length);
}
var rand = function(min, max){
if (typeof min == 'undefined' && typeof max == 'undefined') {
return Math.random();
}
return Math.floor(Math.random() * max) + min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment