Course outline
Full-Stack course / JavaScript fundamentals

Shape and filter weather readings

Lab

How to fetch and submit this exercise

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each 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 submit

The runner depends on the exercise: JavaScript/TypeScript/React labs run with Vitest, Python labs with pytest, and config-file labs (Docker, CI, Terraform, Nginx) are checked by parsing your file. gonomicon test runs the visible checks on your computer first; submitting also runs the hidden checks on our server.

Shape 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

  • celsius exactly 0 does not count as above freezing.
  • Neither function changes (mutates) the readings array or any reading object inside it - both return something new or an existing reading, without altering the input.
  • warmestDay returns 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

  1. 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.
  2. celsius > 0 is strictly above freezing - 0 itself should not pass the test.
  3. 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.