Skip to content

Instantly share code, notes, and snippets.

@iCloud
Last active February 3, 2016 15:10
Show Gist options
  • Save iCloud/9904d7940c2bbfd620c8 to your computer and use it in GitHub Desktop.
Save iCloud/9904d7940c2bbfd620c8 to your computer and use it in GitHub Desktop.
Snippets for CodeWars
// Arrays
Array.prototype.square = function () { return this.map(function(n) { return n*n; }); }
Array.prototype.cube = function () { return this.map(function(n) { return n*n*n; }); }
Array.prototype.average = function () { return this.sum() / this.length; }
Array.prototype.sum = function () { return this.reduce(function(a, b) { return a + b; }, 0); }
Array.prototype.even = function () { return this.filter(function(item) { return 0 == item % 2; }); }
Array.prototype.odd = function () { return this.filter(function(item) { return 0 != item % 2; }); }
Array.prototype.max = function () { return Math.max.apply(null, this); }
Array.prototype.min = function () { return Math.min.apply(null, this); }
//Arguments to array
var array = Array.prototype.slice.call(arguments, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment