Skip to content

Instantly share code, notes, and snippets.

@bennetthardwick
Last active January 7, 2018 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennetthardwick/830d793e53c2dbf674c651022dbd739f to your computer and use it in GitHub Desktop.
Save bennetthardwick/830d793e53c2dbf674c651022dbd739f to your computer and use it in GitHub Desktop.
Some Array prototypes that I often find useful.
// Find the range of a numerical array
// Array.range()
Array.prototype.range = function() { return this.reduce((a, b) => Math.max(a, b)) - this.reduce((a, b) => Math.min(a, b)); };
// Find the sum of a numerical array
// Array.sum()
Array.prototype.sum = function() { return this.reduce((a, b) => a + b); };
// Find the average of a numerical array
// Array.mean()
Array.prototype.mean = function() { return this.reduce((a, b) => (a + b) / 2 )};
// [1, 2, 3, 4].sum() = 10
// [1, 2, 3, 4].range() = 3
// [1, 2, 3, 4].mean() = 3.125
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment