Skip to content

Instantly share code, notes, and snippets.

@charlesdarkwind
Created June 18, 2018 00:55
Show Gist options
  • Save charlesdarkwind/81d61e39de2ba8f8cec7d6b8eb3a6f0f to your computer and use it in GitHub Desktop.
Save charlesdarkwind/81d61e39de2ba8f8cec7d6b8eb3a6f0f to your computer and use it in GitHub Desktop.
const getAvgSpread = prices => {
let totalQty = 0, totalPrices = 0;
for (let price in prices) {
const qty = prices[price];
totalQty += qty;
totalPrices += parseFloat(price) * qty;
}
const avgPrice = parseFloat((totalPrices / totalQty).toFixed(8));
return { avgPrice, totalQty };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment