Shape and filter weather readings
How to fetch and submit this exercise
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach exercise: download it, edit the file named in the instructions, then submit.
gonomicon fetch fs-week-2/03-01-shape-weather-readings
cd 03-01-shape-weather-readings
gonomicon submitShape and filter weather readings
Write two functions that work on a list of daily temperature readings: one that keeps only the readings above freezing, and one that finds the single warmest reading.
Worked example
Here's a similar pair of functions for a list of hourly traffic counts:
const trafficCounts = [
{ hour: 8, cars: 42 },
{ hour: 9, cars: 95 },
{ hour: 10, cars: 30 },
];
function busyHours(counts, threshold) {
return counts.filter((entry) => entry.cars >= threshold);
}
function busiestHour(counts) {
let busiest = counts[0];
for (const entry of counts) {
if (entry.cars > busiest.cars) {
busiest = entry;
}
}
return busiest;
}
console.log(busyHours(trafficCounts, 50));
console.log(busiestHour(trafficCounts));
[ { hour: 9, cars: 95 } ]
{ hour: 9, cars: 95 }
busyHours keeps every entry whose cars count meets the threshold, in the order they started in. busiestHour walks the list once, only replacing busiest when it finds something with a strictly higher count - so if two hours ever tied, the first one found would stay busiest.
Your task
Write function aboveFreezing(readings). Each reading is an object { day, celsius }. It returns a new array with only the readings whose celsius is strictly greater than 0, in the same order they came in.
Write function warmestDay(readings). It returns the single reading object with the highest celsius value. If the list is empty, it returns undefined instead of crashing. If two or more readings tie for warmest, it returns whichever one appears first in the list.
Rules
celsiusexactly0does not count as above freezing.- Neither function changes (mutates) the
readingsarray or any reading object inside it - both return something new or an existing reading, without altering the input. warmestDayreturns the actual reading object from the list, not just a number or a rebuilt copy.
What you'll practice
Using .filter() to build a new array that keeps only matching items in their original order, and writing a comparison loop that tracks the best item seen so far - including what to do with an empty list.
Run it
gonomicon fetch fs-week-2/03-01-shape-weather-readings
cd 03-01-shape-weather-readings
gonomicon test
gonomicon submit
Hints
- filter(...) already keeps the items in their original order, so you don't need to do anything extra to preserve order - just write the test each reading has to pass.
- celsius > 0 is strictly above freezing - 0 itself should not pass the test.
- For warmestDay, walk the list keeping track of the best reading seen so far, and only replace it when you find something strictly warmer - that's what makes the first of two tied readings win.