Taking Remainders With %

With Math.floor() in place of //, the port of batch_plan.py counts three full batches. The head roaster also needs the last part batch, which Python gets from %. JavaScript’s % agrees for the planner’s weights. With a positive divisor, the two part ways once the left side goes negative.

Port the Last Part Batch

Python’s last_batch_kg = green_kg % DRUM_KG ports with only the usual const and camelCase changes, and like Python’s, JavaScript’s % works on decimals:

Language: JavaScript
const DRUM_KG = 12;

const greenKg = 37.5 / (1 - 0.15);
const fullBatches = Math.floor(greenKg / DRUM_KG);
const lastBatchKg = greenKg % DRUM_KG;
console.log(`Batches: ${fullBatches} full + ${lastBatchKg} kg`);

Both scripts print the same line:

Language: Program Output
Batches: 3 full + 8.117647058823529 kg

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

Become a Member to join the conversation.