The bookkeeper’s script opens with October’s total weight and how many 12 kg drum loads it took. In Python, that line rests on sum(), which does more careful work than its name suggests. Does the JavaScript port count the same drums?
Count the Drum Loads
A JavaScript number is a 64-bit floating-point number, or double, like Python’s float, so 0.1 + 0.2 is 0.30000000000000004 in both, and (0.1).toFixed(20) shows the same stored "0.10000000000000000555" as f"{0.1:.20f}". What differs is what each language gives you for adding and comparing them.
Python totals the weights with sum(order["kg"] for order in orders) and rounds up with math.ceil(), printing 6 drum loads. JavaScript has no sum(), though arrays have a .reduce() method that adds the same way as +=. You’ll cover that method later. For now, a line-by-line port uses a for...of loop with +=:
const MONTH = "October 2026";
const DRUM_KG = 12;
const orders = [
{ cafe: "Larder Lane", kg: 12.6 },
{ cafe: "Larder Lane", kg: 11.6 },
{ cafe: "Bluebell Café", kg: 4.8 },
{ cafe: "Northside Kitchen", kg: 9.7 },
{ cafe: "Bluebell Café", kg: 4.2 },
{ cafe: "Northside Kitchen", kg: 8.9 },
{ cafe: "Larder Lane", kg: 11.8 },
{ cafe: "Northside Kitchen", kg: 8.4 },
];
let totalKg = 0;
for (const order of orders) {
totalKg += order.kg;
}
const drumLoads = Math.ceil(totalKg / DRUM_KG);
console.log(
`${MONTH}: ${totalKg.toFixed(1)} kg in ${orders.length} ` +
`orders, ${drumLoads} drum loads`,
);
Before you run it, how many drum loads do you expect? Seventy-two kilos is six drums, but the port says seven:
October 2026: 72.0 kg in 8 orders, 7 drum loads
Each += rounds to the nearest double, and eight roundings leave the total at 72.00000000000001. .toFixed(1) hides that, as :.1f would, but Math.ceil() sees the stored value. Python’s own += loop gives the same total. The difference is sum(), which, in Python 3.12 and later, compensates for each addition’s rounding and returns exactly 72.0 here.
Add Up Grams Instead of Kilos
The scale reads to 0.1 kg, so every weight is a whole number of grams, and whole numbers add exactly in a double when their sum is below 2 ** 53, a limit for a later lesson. Convert the weights as they come in, with Math.round() because a product like 16.1 * 1000 can land a hair off (16100.000000000002). You change one line in the loop, multiply DRUM_KG by 1000 for the drum count, and convert back to kilos for display:
const MONTH = "October 2026";
const DRUM_KG = 12;
const orders = [
{ cafe: "Larder Lane", kg: 12.6 },
{ cafe: "Larder Lane", kg: 11.6 },
{ cafe: "Bluebell Café", kg: 4.8 },
{ cafe: "Northside Kitchen", kg: 9.7 },
{ cafe: "Bluebell Café", kg: 4.2 },
{ cafe: "Northside Kitchen", kg: 8.9 },
{ cafe: "Larder Lane", kg: 11.8 },
{ cafe: "Northside Kitchen", kg: 8.4 },
];
let totalGrams = 0;
for (const order of orders) {
totalGrams += Math.round(order.kg * 1000);
}
const drumLoads = Math.ceil(totalGrams / (DRUM_KG * 1000));
const totalKg = totalGrams / 1000;
console.log(
`${MONTH}: ${totalKg.toFixed(1)} kg in ${orders.length} ` +
`orders, ${drumLoads} drum loads`,
);
Whole grams add up to exactly 72,000, and the report line now matches Python’s:
October 2026: 72.0 kg in 8 orders, 6 drum loads
Compare With a Tolerance That Fits the Data
Sometimes you can’t choose the unit. The order dashboard, for example, sends its own total, already added in kilos. JavaScript has no math.isclose(), so write the check yourself, with a tolerance taken from the data. Half the scale’s resolution is 0.05 kg. Here, the sum of the eight weights stands in for the dashboard’s total, so the check reads:
const totalKg = 12.6 + 11.6 + 4.8 + 9.7 + 4.2 + 8.9 + 11.8 + 8.4;
console.log(totalKg, Math.abs(totalKg - 72) < 0.05);
The total keeps its error but counts as 72 kg:
72.00000000000001 true
That’s equivalent to Python’s abs(total_kg - 72.0) < 0.05, a fixed allowance, while math.isclose() scales its tolerance to the values by default. Don’t use Number.EPSILON as the tolerance. It’s the gap between 1 and the next double, far smaller than the gap near 72, so Math.abs(totalKg - 72) < Number.EPSILON is false.
The JavaScript way is to sum measured values in whole units from the moment they arrive and convert back only for display. Compare computed floats with a tolerance that means something for the data. .toFixed() returns a string for printing and leaves the number, error included, as it was.
Next up is Counting Money in Cents, where you’ll port the script’s money lines, which Python keeps in decimal.Decimal, and check the month’s total against the web shop’s to the cent.