Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Porting the Readings Check

Porting the Readings Check

In this lesson, you ported most of the co-op’s Python readings check to JavaScript, line by line, keeping its logic and changing its comparisons and type checks. Then you checked the file and ran both versions side by side.

  • BATCH becomes a const array of objects. The capitals stay, and const adds what Python’s capitals only ask for: the name can’t be reassigned, though the array can still change, like a Python list. The keys lose their quotes, and two-word keys turn camelCase: takenAt, lastTemp.
  • datetime(2026, 11, 4, 9, 0) becomes new Date("2026-11-04T09:00"), math.nan becomes NaN, and None becomes null. School’s time stays the text "not set", as in Python.
  • rain_label() becomes rainLabel(), with each test on one line with its return. is None becomes === null, checked first, and == 0 becomes === 0. The zero test stays strict: Python’s rain == 0 never converts text, and === doesn’t either. This batch never sends text, unlike the equality lesson’s 12:00 readings, so nothing needs converting.
  • Python’s conditional expression becomes a let and an if: the text starts as the temperature, and Number.isNaN() swaps in "no temperature". JavaScript has a shorter form, but conditionals get a course of their own.
  • The same_temp() helper goes. Object.is() already counts NaN as the same as itself, which is all the helper added to ==. The two part only at minus zero and zero, which Object.is() tells apart, and no station sends minus zero. So orchard’s temperature, NaN twice, comes out as no temperature (unchanged).
  • The loop keeps names such as "ridge #1412" in seen, not readings: includes() compares objects by identity, so it would never find the resent copy.
  • Python’s two isinstance() checks split by kind. The station is text, a primitive, so typeof checks it: instanceof String is false for every station name, where Python’s isinstance() gives True. The time is a Date, so instanceof checks it, negated as a whole, !(reading.takenAt instanceof Date). Without the parentheses, the ! would apply to the time alone, and school’s reading would get through.
  • or becomes ||, and each described reading counts with checked += 1.
  • node --check readings-check.js prints nothing when there’s no syntax error.

Here’s the port so far, readings-check.js, without the place lines and the count line:

Language: JavaScript
const BATCH = [
  { station: "ridge", seq: 1412,
    takenAt: new Date("2026-11-04T09:00"),
    temp: 6.5, lastTemp: 6.1, rain: 0.2,
    location: { lat: 54.61, lon: -3.05 } },
  { station: "mill", seq: 877,
    takenAt: new Date("2026-11-04T09:00"),
    temp: 7.4, lastTemp: 7.4, rain: 0,
    location: { lat: 54.58, lon: -3.11 } },
  { station: "ridge", seq: 1412,
    takenAt: new Date("2026-11-04T09:00"),
    temp: 6.5, lastTemp: 6.1, rain: 0.2,
    location: { lat: 54.61, lon: -3.05 } },
  { station: "orchard", seq: 2290,
    takenAt: new Date("2026-11-04T09:00"),
    temp: NaN, lastTemp: NaN,
    rain: null, location: null },
  { station: "school", seq: 96,
    takenAt: "not set",
    temp: 5.8, lastTemp: 5.9, rain: 0.4,
    location: { lat: 54.6, lon: -3.08 } },
];

function rainLabel(rain) {
  if (rain === null) return "no rain data";
  if (rain === 0) return "dry";
  return `${rain} mm rain`;
}

function describe(reading) {
  const temp = reading.temp;
  let text = `${temp}°C`;
  if (Number.isNaN(temp)) text = "no temperature";
  if (Object.is(temp, reading.lastTemp)) text += " (unchanged)";
  const rain = rainLabel(reading.rain);
  return `${text}, ${rain}`;
}

const seen = [];
let checked = 0;
for (const reading of BATCH) {
  const name = `${reading.station} #${reading.seq}`;
  if (seen.includes(name)) {
    console.log(`Skipped resent ${name}`);
  } else {
    seen.push(name);
    const badStation = typeof reading.station !== "string";
    const badTime = !(reading.takenAt instanceof Date);
    if (badStation || badTime) {
      console.log(`Rejected ${name}: bad fields`);
    } else {
      console.log(`${name}: ${describe(reading)}`);
      checked += 1;
    }
  }
}

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 You’ll port most of the co-op’s readings check to JavaScript. Every hour, this Python script skips a resent reading, rejects bad fields, and describes the rest: the temperature, the rain and the place.

00:14 The website runs on JavaScript, so the check moves there too. The logic stays, and what changes is each comparison and each type check. The port must print Python’s six lines, once the exercise after this video finishes it.

00:31 In JavaScript, BATCH becomes a const array of objects. The capitals stay, and const adds what Python’s capitals only ask for: the name can’t be reassigned, though the array can still change, like a Python list.

00:46 The keys lose their quotes, and a two-word key such as last_temp turns camelCase. The other four readings follow the same way. Three kinds of value change.

00:59 The datetime becomes a new Date() with the same time. math.nan becomes NaN, None becomes null, and school’s time stays text.

01:10 Next, rain_label() becomes rainLabel(), in camelCase. Each test fits on one line with its return, so it needs no braces.

01:22 is None becomes === null, checked first, as in Python. Then == 0 becomes === 0, and the f-string becomes a template literal.

01:34 The zero test stays strict: Python’s rain == 0 never converts text, and === doesn’t either. This batch never sends text, unlike the 12:00 readings, so nothing needs converting.

01:50 In describe(), Python’s conditional expression becomes a let and an if statement: the text starts as the temperature, and Number.isNaN() swaps in “no temperature”.

02:02 JavaScript has a shorter form, but conditionals get a course of their own. The same_temp() helper goes. Object.is() already counts NaN as the same as itself, which is all the helper added.

02:17 The two part only at minus zero and zero, which Object.is() tells apart, and no station sends minus zero.

02:26 Orchard’s thermometer failed, so it sent NaN both times. What text does describe() build for orchard’s temperature? Your turn: pause the video and predict it.

02:42 No temperature, unchanged.

02:46 Number.isNaN() caught the NaN, and Object.is() matched the pair. The rain line carries over. The place lines are left for the exercise after this video, so for now the return joins the text and the rain.

03:03 The loop keeps names in seen, not readings: includes() compares objects by identity, so it would never find the resent copy.

03:13 Python’s two isinstance() checks split by kind. The station is text, a primitive, so typeof checks it: where Python’s isinstance() gives True, instanceof String gives false for every station name.

03:28 The time is a Date, so instanceof checks it, inside parentheses, with the ! outside: without them, the ! would apply to the time alone, and school would get through.

03:42 or becomes ||, and each described reading counts. The line that prints the count is the exercise’s too.

03:51 node --check reads the whole file without running it. It prints nothing, so there’s no syntax error. Now run both. The skip and the rejection match exactly.

04:07 Ridge, mill and orchard match up to the place. And the count line is still to come.

04:15 The logic stayed, and the comparisons and type checks changed. Strict equality for null and zero, Number.isNaN() and Object.is() for NaN, and names kept in seen for the skip.

04:29 Then typeof for a primitive, and instanceof for a class, negated in parentheses. Next up is the exercise Finish the Readings Check.

04:40 You’ll add the place each reading reports, its coordinates or no GPS, and the count of readings checked. Then the port prints Python’s six lines, for this batch and for a second one the tests make up.

Become a Member to join the conversation.