Skip to content

Instantly share code, notes, and snippets.

@alvarotrigo
Last active January 30, 2020 17:11
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 alvarotrigo/39c8fd6ea60fbd2055f47a0893c5ef8b to your computer and use it in GitHub Desktop.
Save alvarotrigo/39c8fd6ea60fbd2055f47a0893c5ef8b to your computer and use it in GitHub Desktop.
(function(){
init();
function init(){
var year = '2019';
var months = [];
var rowsForGivenYear = [];
// Filtering the row for the given year
document.querySelectorAll('li.row').forEach(function(row){
var period = row.querySelector('h4');
if(period && period.innerHTML.indexOf(year) > -1 ){
rowsForGivenYear.push(row);
}
});
// Iterating over each month
rowsForGivenYear.forEach(function(row){
var period = row.querySelector('h4').innerHTML;
var amounts = row.querySelectorAll('.amounts li');
var sales = getAmount(amounts[0]);
var refunds = getAmount(amounts[1]);
var fees = getAmount(amounts[1]);
var total = parseFloat( (sales - refunds - fees).toFixed(2));
months.push({
period: period,
total: total,
sales: sales,
refunds: refunds,
fees: fees
});
});
// Verifying we got the correct values
console.log(months);
// Summing all totals
var income = months.reduce(function(prev, current, index){
console.log(current.period);
console.log(prev + ' + ' + current.total);
return prev + current.total;
}, 0);
console.warn('-------------- TOTAL --------------')
console.log({income});
}
// Gets the number value of the amount text
function getAmount(element){
var text = element.querySelector('span').innerHTML;
var value = text.replace(' USD', '').replace('$', '').replace(',','');
return parseFloat(parseFloat(value).toFixed(2));
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment