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.
BATCHbecomes aconstarray of objects. The capitals stay, andconstadds 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)becomesnew Date("2026-11-04T09:00"),math.nanbecomesNaN, andNonebecomesnull. School’s time stays the text"not set", as in Python.rain_label()becomesrainLabel(), with each test on one line with itsreturn.is Nonebecomes=== null, checked first, and== 0becomes=== 0. The zero test stays strict: Python’srain == 0never 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
letand anif: the text starts as the temperature, andNumber.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 countsNaNas the same as itself, which is all the helper added to==. The two part only at minus zero and zero, whichObject.is()tells apart, and no station sends minus zero. So orchard’s temperature,NaNtwice, comes out asno temperature (unchanged). - The loop keeps names such as
"ridge #1412"inseen, 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, sotypeofchecks it:instanceof Stringisfalsefor every station name, where Python’sisinstance()givesTrue. The time is aDate, soinstanceofchecks 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. orbecomes||, and each described reading counts withchecked += 1.node --check readings-check.jsprints nothing when there’s no syntax error.
Here’s the port so far, readings-check.js, without the place lines and the count line:
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;
}
}
}