Problems › JavaScript › machines
Good parts per run cycle
Each cycle a press makes partsPerCycle units and chips away scrapPerCycle of them. Count the good units over the whole run.
- Each cycle yields partsPerCycle minus scrapPerCycle good units.
- The good units are never negative — if scrap matches or beats output the cycle yields zero.
- The total is good units times the number of cycles.
cycleOutput(cycles: int, partsPerCycle: int, scrapPerCycle: int) → int
Where you start
function cycleOutput(cycles, partsPerCycle, scrapPerCycle) {
}
Worked examples
| Call | Result |
|---|---|
cycleOutput(10, 5, 1) | 40 |
cycleOutput(0, 5, 1) | 0 |
cycleOutput(5, 3, 3) | 0 |
cycleOutput(5, 3, 5) | 0 |
Hint
Clamp the per-cycle good count at zero, then multiply by the cycles.
Reference solution in JavaScript
function cycleOutput(cycles, partsPerCycle, scrapPerCycle) {
const net = Math.max(0, partsPerCycle - scrapPerCycle);
return cycles * net;
}