Skip to content

Instantly share code, notes, and snippets.

@titaniumbones
Last active March 17, 2020 15:03
Show Gist options
  • Save titaniumbones/445ef5a375ffbdc4c13d9fe9be30fe37 to your computer and use it in GitHub Desktop.
Save titaniumbones/445ef5a375ffbdc4c13d9fe9be30fe37 to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Document</title>
</head>
<body>
<script>
function quickRound (n) {
m = n* 100
r = Math.round(m)
return r / 100
}
fetch("https://pomber.github.io/covid19/timeseries.json")
.then(response => response.json())
.then(data => {
let previous = 0;
let lastSeven = []
data["Canada"].forEach( (entry) => {
//console.log(entry);
let { date, confirmed, recovered, deaths } = entry;
let active = confirmed - recovered - deaths;
let growthrate = (previous > 0) ? (active / previous) : ""
console.log(`${date} active cases: ${confirmed - recovered - deaths}`)
let row = document.querySelector('#can-data').insertRow();
for (let i of [date, active, recovered, deaths, quickRound(growthrate)]) {
row.insertCell().appendChild(document.createTextNode(i));
}
previous = active;
if (Number.isFinite(growthrate)) {
lastSeven.push(growthrate);
}
if (lastSeven.length > 7) {
lastSeven.shift();
}
if (lastSeven.length === 7 ) {
let sum = lastSeven.reduce((a,b ) => a + b, 0)
console.log(date, sum / 7)
}
}
)
})
</script>
<table id="can-data">
<tr>
<th>Date</th>
<th>Active Cases</th>
<th>Recovered</th>
<th>Deaths</th>
<th>weekly growthrate</th>
</tr>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment