Created
May 5, 2016 04:07
-
-
Save danielrobertson/30d0a0699a8a41d197b7d809cb67c319 to your computer and use it in GitHub Desktop.
Parses a CSV with daily calories in/out and calculates the average daily caloric deific
This file contains hidden or 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 parse = require('csv-parse'); | |
var consumed = 0; | |
var burned = 0; | |
var numDays = 0; | |
var parser = parse({delimiter: ';'}, function(err, data){ | |
data.forEach(function(data){ | |
++numDays; | |
calories = String(data).split(","); | |
consumed += Number(calories[0]); | |
burned += Number(calories[1]); | |
}); | |
console.log("Summary for " + numDays + " days:"); | |
console.log("Average daily consumption, burned = " + Math.round(consumed / numDays) + ", " + Math.round(burned / numDays)); | |
console.log("Average daily deficit = " + Math.round((burned - consumed) / numDays)); | |
}); | |
fs.createReadStream(__dirname+'/calories.csv').pipe(parser); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CSV should have two columns, first calories consumed, then calories burned. E.g.
1833,2346
2581,2627
1901,2758