Skip to content

Instantly share code, notes, and snippets.

@dance2die
Created March 31, 2018 18:19
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 dance2die/0013a7255e993ac11914e663799fd8c8 to your computer and use it in GitHub Desktop.
Save dance2die/0013a7255e993ac11914e663799fd8c8 to your computer and use it in GitHub Desktop.
Array.prototype.average = function(propertySelector = obj => obj) {
const intialValue = 0;
return (
this.reduce((sum, obj) => sum + propertySelector(obj), intialValue) /
this.length
);
};
function averageDemo(orders) {
var averageQuantity = orders.average(order => order.quantity);
var totalQuantities = orders.sum(order => order.quantity);
var count = orders.length;
WriteLine(
`Average Quantity: ${totalQuantities} / ${count} = ${averageQuantity}`
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment