Problems › JavaScript › production
How many batches to run
The mixer takes a fixed number of units per batch, and a part-full batch still has to be run.
- A leftover of even one unit needs a whole extra batch.
- A batch size of zero or less makes no sense: return 0.
- Nothing to make means no batches.
batchesNeeded(units: int, perBatch: int) → int
Where you start
function batchesNeeded(units, perBatch) {
}
Worked examples
| Call | Result |
|---|---|
batchesNeeded(100, 25) | 4 |
batchesNeeded(101, 25) | 5 |
batchesNeeded(1, 25) | 1 |
batchesNeeded(0, 25) | 0 |
Hint
Ceiling division in integers: (units + perBatch - 1) / perBatch.
Reference solution in JavaScript
function batchesNeeded(units, perBatch) {
if (units <= 0 || perBatch <= 0) return 0;
return Math.floor((units + perBatch - 1) / perBatch);
}