Skip to content

Instantly share code, notes, and snippets.

@danielrobertson
Created May 5, 2016 04:07
Show Gist options
  • Save danielrobertson/30d0a0699a8a41d197b7d809cb67c319 to your computer and use it in GitHub Desktop.
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
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);
@danielrobertson
Copy link
Author

CSV should have two columns, first calories consumed, then calories burned. E.g.
1833,2346
2581,2627
1901,2758

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment