Last active
July 20, 2016 00:24
-
-
Save merryt/9edb4656b64b24a514c697802d9677f8 to your computer and use it in GitHub Desktop.
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
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