The roastery’s order dashboard runs on JavaScript, so the batch planner is moving to batch-plan/batch-plan.js, next to a package.json holding { "type": "module" }. The Python script relies on two number types and on Python stopping when a division makes no sense. JavaScript keeps neither promise.
Think Number, Not int or float
JavaScript has one type, number, for whole and decimal values alike. Where Python would answer int, float, and 5.0, the REPL, which you start with node, says this:
> typeof 12
'number'
> typeof 17.5
'number'
> 10 / 2
5
Every number is a 64-bit float, and a whole number is a float with no fractional part, so it prints without a .0. Since typeof can’t tell 3 from 3.0, wholeness is a question about the value, and Number.isInteger() asks it. Here it checks whether tomorrow’s green beans fill the 12 kg drum a whole number of times:
const ORDER_KG = 37.5;
const WEIGHT_LOSS = 0.15;
const DRUM_KG = 12;
const greenKg = ORDER_KG / (1 - WEIGHT_LOSS);
console.log(Number.isInteger(greenKg / DRUM_KG));
console.log(Number.isInteger(36 / DRUM_KG));
The 44.1 kg doesn’t, though 36 kg would:
false
true
The rest of the planner’s arithmetic maps like this:
| Python | JavaScript | Difference |
|---|---|---|
+ - * / ** |
+ - * / ** |
Same spelling, and / is always true division (division by zero comes below) |
1_000_000 |
1_000_000 |
None |
-2 ** 2 gives -4 |
-2 ** 2 |
SyntaxError: write -(2 ** 2) for Python’s -4, or (-2) ** 2 for 4 |
x.is_integer() |
Number.isInteger(x) |
Called on Number, with the value passed as its argument |
Number.isInteger() checks the computed float, so a rounding error can make an exact batch count appear fractional. You’ll learn about float precision, the largest exact whole number, and BigInt in a later course.
Guard Sunday’s Average
The dashboard also shows the average order weight, 12.5 kg over tonight’s three lines. On Sunday the export is empty, so Python’s total_kg / line_count is 0 / 0 and the script stops with ZeroDivisionError: division by zero. Here’s the port with Sunday’s numbers, in a template literal, where ${} plays the role of an f-string’s braces:
const totalKg = 0;
const lineCount = 0;
console.log(`Average order: ${totalKg / lineCount} kg`);
console.log("Plan finished");
Does JavaScript stop too? It doesn’t even warn:
Average order: NaN kg
Plan finished
Python’s float has the same nan and inf, but Python checks for a zero divisor and raises an exception. JavaScript doesn’t check, so arithmetic on numbers never throws. NaN, “not a number,” is the answer to 0 / 0, though typeof NaN is "number". A non-zero number over zero gives Infinity or -Infinity. Either flows on into later lines and surfaces far from the division.
The JavaScript way is to do Python’s check yourself, before dividing. The if takes its condition in parentheses and each branch in braces:
const totalKg = 0;
const lineCount = 0;
if (lineCount === 0) {
console.log("No orders in today's export");
} else {
console.log(`Average order: ${totalKg / lineCount} kg`);
}
With Sunday’s zeros, the if branch runs:
No orders in today's export
Next up is Replacing // With Math.floor(), where you’ll port the line that counts full batches, Python’s int(green_kg // DRUM_KG), and learn to say which rounding you mean.