Last active
January 30, 2020 17:11
-
-
Save alvarotrigo/39c8fd6ea60fbd2055f47a0893c5ef8b 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
(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