Skip to content

Instantly share code, notes, and snippets.

@ryankuykendall
Last active March 21, 2020 20:37
Show Gist options
  • Save ryankuykendall/033592c8766d3b50ec03037f566135b6 to your computer and use it in GitHub Desktop.
Save ryankuykendall/033592c8766d3b50ec03037f566135b6 to your computer and use it in GitHub Desktop.
/** This can be replace with Intl.NumberFormatter */
commafy = (num) => {
const [whole, decimal] = num.toString().split('.');
const collection = [];
const places = whole.split('');
while (places.length > 0) {
const three = [];
for (let i = 0 ; i < 3 ; i++) {
if (places.length) {
three.unshift(places.pop());
}
}
if (three.length > 0) {
collection.unshift(three.join(''));
}
}
let formattedNumber = collection.join(',');
if (decimal) {
formattedNumber += `.${decimal}`;
}
return formattedNumber;
}
/** commafy can be replaced by Intl.NumberFormatter! */
growth = (factor = 1.01, weeks = 52, infected = 109_000, mortality = 0.035, population = 7_600_000_000) => {
const now = new Date();
const formatter = new Intl.NumberFormat();
for (let i = 1 ; i <= weeks ; i++) {
infected = Math.round(infected * factor);
const deaths = Math.round(infected * mortality);
const week = new Date(now.setDate(now.getDate() + 7));
console.log("Week", (i).toString().padStart(3), week.toDateString(),
"\t",
"infected", formatter.format(infected).padStart(16),
"\t\t",
"deaths", formatter.format(deaths).padStart(12));
if (infected > population) break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment