Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var stats = document.getElementsByClassName('stats-both');
var firstOnlyStats = document.getElementsByClassName('stats-firstonly');
var total = parseInt(stats[stats.length-2].textContent, 10) + parseInt(firstOnlyStats[firstOnlyStats.length-2].textContent, 10);
for (var i = 0; i < stats.length; i++)
{
var num = parseInt(stats[i].textContent, 10);
var numBoth = parseInt(firstOnlyStats[i].textContent, 10) + num;
if (!isNaN(num))
{
var percent = ""+Math.round(num*100/total);
var percentBoth = ""+Math.round(numBoth*100/total)
percent = `(${percent.padStart(2, ' ')}%)`;
percentBoth = `(${percentBoth.padStart(3, ' ')}%)`;
if (num === 0)
percent = "".padStart(5, ' ');
if (numBoth === 0)
percentBoth = "".padStart(5, ' ');
stats[i].textContent = `${stats[i].textContent} ${percent}`;
firstOnlyStats[i].textContent = `${firstOnlyStats[i].textContent} ${percentBoth}`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment