groundhogs = {
const response = await fetch("https://groundhog-day.com/api/v1/groundhogs");
const json = await response.json();
const groundhogs = json.groundhogs;
return groundhogs
.map((groundhog) => ({
...groundhog,
lat: parseFloat(groundhog.coordinates?.split(", ")[0]),
lon: parseFloat(groundhog.coordinates?.split(",")[1])
}))
.map((groundhog) => ({
...groundhog,
predictions: groundhog.predictions.map((prediction) => {
const conclusion =
prediction.shadow === 1
? "❄️ longer winter"
: prediction.shadow === 0
? "🌼 early spring"
: undefined;
const year = new Date("2/2/" + prediction.year);
const correspondingWeather = weather.find(
(w) => w.year?.getTime() === year.getTime()
);
const correct =
correspondingWeather !== undefined &&
correspondingWeather.skew !== undefined &&
conclusion !== undefined
? correspondingWeather.skew === conclusion
: undefined;
return {
...groundhog,
...prediction,
year,
conclusion,
correct
};
})
}))
.map((groundhog) => ({
...groundhog,
accuracy: Number(
(
groundhog.predictions.filter((p) => p.correct === true).length /
groundhog.predictions.filter((p) => p.correct !== undefined).length
).toFixed(2)
)
}));
}