Skip to content

Instantly share code, notes, and snippets.

@merryt
Last active July 20, 2016 00:24
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 merryt/9edb4656b64b24a514c697802d9677f8 to your computer and use it in GitHub Desktop.
Save merryt/9edb4656b64b24a514c697802d9677f8 to your computer and use it in GitHub Desktop.
var fs = require("fs");
var gatherStockData = function(stockData){
parsedData = JSON.parse(stockData)
return parsedData
}
var sortByDecade = function(fileData){
// expected input is filedata = [{"Year": 1928,"S&P 500": "43.81%"}]
var tenPercentBreakdowns = new Map();
fileData.forEach(function(v){
// var roundedValue = Math.round(parseFloat(v["S&P 500"]) / 10) * 10; // rounds to tens place
var roundedValue = Math.round(parseFloat(v["S&P 500"])); // rounds to single digit
var obj = tenPercentBreakdowns.get(roundedValue);
if(obj === undefined)
{
obj = { 'percentWindow': roundedValue, 'count': 0 };
tenPercentBreakdowns.set(roundedValue, obj);
}
obj.count++;
});
return Array.from(tenPercentBreakdowns.values()).sort(function(a,b){
return a.percentWindow - b.percentWindow;
});
}
var fileData = gatherStockData(fs.readFileSync("stockdata.json"));
output = sortByDecade(fileData)
console.log(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment