Public
Edited
Nov 3, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data
Insert cell
Insert cell
Insert cell
{
function applyPercap(row) {
return {
...row,
cases_per_cap: percapita(row.count_cases, row.population),
deaths_per_cap: percapita(row.count_deaths, row.population),
new_cases_per_cap: percapita(row.new_cases, row.population),
new_deaths_per_cap: percapita(row.new_deaths, row.population),
}
}
let newArray = [];
for (let row of data) {
let result = applyPercap(row);
newArray.push(result);
}
return newArray;
}
Insert cell
Insert cell
mapData = {
// Update this to add the necessary percapita calculations just like in the example above
function applyPercap(row) {
return {
...row,
new_cases_per_cap: percapita(row.new_cases, row.population),
}
}

let newArray = [];
for (let row of data) {
let result = applyPercap(row);
newArray.push(result);
}

// Update this to use the array's map method
// If you have done it correctly, a line chart will appear below
return data
}
Insert cell
Insert cell
Insert cell
Insert cell
{
function isInCorrectRange(row) {
let correctPopulation = row.population >= 20000 && row.population <= 50000;
let isDecember = row.date.getMonth() === 11; // getMonth returns a the number of the month as an integer starting at 0
return correctPopulation && isDecember;
}
let filteredArray = [];
for (let row of mapData) {
if (isInCorrectRange(row)) {
filteredArray.push(row)
}
}
return filteredArray;
}
Insert cell
Insert cell
filteredData = {
data.filter(function(x) {
return x.population >= 20000 && x.population <= 50000
})
// Update this to return filtered mapData of only counties with >= 20,000 and <= 50,000 pop
return mapData
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
function countyAvg(acc, row) {
if (!acc[row.county]) {
acc[row.county] = {sum: 0, N: 0}
}
acc[row.county].sum += row.new_deaths;
acc[row.county].N += 1;
return acc;
}
let acc = {};
for (let row of filteredData) {
acc = countyAvg(acc, row)
}
return acc
}
Insert cell
Insert cell
reduceData = {
// This can stay the same
function countyAvg(acc, row) {
if (!acc[row.county]) {
acc[row.county] = {sum: 0, N: 0}
}
acc[row.county].sum += row.new_deaths;
acc[row.county].N += 1;
return acc;
}

return filteredData.reduce(countyAvg, {}) // Use reduce here. Don't forget to supply an initial accumulator object!
}
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more