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

Replacing // With Math.floor()

Replacing // With Math.floor()

In this lesson, you ported the batch planner’s full-batches line, the one that went quietly wrong in the course introduction, and found out why: JavaScript has no floor division operator.

  • In Python, green_kg // DRUM_KG divides and rounds down. On a float it gives 3.0, so the script wraps it in int() to get 3.
  • The port lives in batch-plan.js, in the folder batch-plan/, whose package.json holds { "type": "module" }.
  • JavaScript has one number type, so the port has no int to convert to, and drops the int() call.
  • In JavaScript, // starts a comment, as # does in Python, and the comment runs to the end of the line. So const fullBatches = greenKg // DRUM_KG; is really const fullBatches = greenKg, and it runs with no error.
  • Inside a call’s parentheses, the comment swallows the closing one. Then the file doesn’t run at all: JavaScript reports SyntaxError: missing ) after argument list before running a single line.
  • Division in JavaScript is always /, which keeps the fraction. Math.floor() rounds the result down, as Python’s // does, and shares its name with Python’s math.floor(). Math holds JavaScript’s math functions, like Python’s math module, and it gets a lesson of its own soon.
  • Python’s int() has two jobs. In the batches line it only turned 3.0 into 3, which JavaScript doesn’t need. Given decimals, it chops them off, and that’s Math.trunc(): Math.trunc(greenKg / DRUM_KG) is 3, like int(green_kg / DRUM_KG). For the planner’s weights, which are never negative, it gives the same 3 as Math.floor().
  • Watch out: for values that can go below zero, such as a time offset, the two part ways. Math.floor() goes down the number line and Math.trunc() goes toward zero.
  • So Math.floor(-7 / 2) is -4, like Python’s -7 // 2, while Math.trunc(-7 / 2) is -3, like int(-7 / 2).
  • When you port //, say which rounding you mean, above all when values can go negative: Math.floor() rounds down, as // does, and Math.trunc() chops the decimals, as int() does.

Here’s the port as first carried over from Python, with the // kept:

Language: JavaScript
const WEIGHT_LOSS = 0.15;
const DRUM_KG = 12;

const orderKg = 37.5;
const greenKg = orderKg / (1 - WEIGHT_LOSS);
const fullBatches = greenKg // DRUM_KG;
console.log(`Batches: ${fullBatches} full`);

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:02 Back in the course introduction, one line of the JavaScript port went quietly wrong. In this lesson, you’ll port that line yourself, find out why, and fix it.

00:14 The line counts how many full twelve kilo batches the drum takes. Here are the Python script’s batch lines, with the order typed in as a number for now. Python’s // divides the green beans by the drum and rounds down.

00:30 On a float, it still gives a float, 3.0, so int() turns it into a whole number. The script prints three full batches.

00:41 The port lives in batch-plan.js, in a folder whose package.json sets the module type. The settings and the green-bean line carry over, with const, camel case names and semicolons.

00:54 Then the batches line. JavaScript has one number type, so there’s no int to convert to, and the port drops the int() call. The division keeps its //, and the print line becomes console.log() with a template literal.

01:11 Python printed three full batches. What will JavaScript print when you run the file, and why? Your turn: pause the video and predict.

01:26 The whole green-bean weight, decimals and all, as the number of full batches. There’s no error and no warning. The division by the drum never happened.

01:39 JavaScript has no floor division operator. // starts a comment, as # does in Python, and runs to the end of the line. Your editor colors it like any other comment.

01:53 JavaScript reads the line as const fullBatches = greenKg, and the batches get the whole weight. Inside a function call’s parentheses, the comment would swallow the closing one.

02:06 Then the file wouldn’t run at all: JavaScript reports a SyntaxError before running a single line. Here, what’s left is still valid code, so nothing warns you.

02:20 Division in JavaScript is always a single slash, which keeps the fraction. That’s a little over three and a half drums. To round that down, as // does, the fix wraps Math.floor() around the division.

02:36 Math holds JavaScript’s math functions, like Python’s math module. It gets a lesson of its own soon. Math.floor() and Python’s math.floor() even share a name.

02:48 Run the file again. Now it prints three full batches, as Python did.

02:55 Python’s int() has two jobs. In the batches line, it turned 3.0 into 3, which JavaScript doesn’t need. Given decimals, it chops them off, and JavaScript’s Math.trunc(), short for truncate, does the same.

03:12 So these two lines divide with a single slash on purpose, to leave decimals to chop. On the batches, both give three, the same count as the fix. For the planner’s weights, which are never negative, Math.floor() and Math.trunc() always agree.

03:31 Watch out, though. For values that can go below zero, such as a time offset, the two part ways. On the number line, starting from -7 / 2, three and a half below zero, Math.floor() goes down, and Math.trunc() goes toward zero.

03:49 Math.floor(-7 / 2) is -4, and Python’s -7 // 2 gives the same. Math.trunc(-7 / 2) is -3, and Python’s int(-7 / 2) agrees.

04:07 So when you port //, say which rounding you mean, above all when values can go negative. Math.floor() rounds down, as // does, and Math.trunc() chops the decimals, as int() does.

04:22 The batches line keeps Math.floor(), because a count of full batches always rounds down.

04:29 Next is the text lesson Taking Remainders With %, where you’ll port the size of the last part batch. You’ll also see JavaScript’s % part ways with Python’s on a negative left side.

Become a Member to join the conversation.