Last active
January 7, 2018 13:06
-
-
Save bennetthardwick/830d793e53c2dbf674c651022dbd739f to your computer and use it in GitHub Desktop.
Some Array prototypes that I often find useful.
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
// 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