Problems › JavaScript › machines
Average downtime per stop
Support wants a single number for "how long stops usually take", averaged over every recorded event.
- The average is the total duration divided by the count, truncated down.
- With no events at all the average is treated as zero.
meanDowntime(durations: list<int>) → int
Where you start
function meanDowntime(durations) {
}
Worked examples
| Call | Result |
|---|---|
meanDowntime([10,20,30]) | 20 |
meanDowntime([5,6]) | 5 |
meanDowntime([]) | 0 |
meanDowntime([100]) | 100 |
Hint
Sum then divide by the length; guard the empty case.
Reference solution in JavaScript
function meanDowntime(durations) {
if (!durations.length) return 0;
let sum = 0;
for (const d of durations) sum += d;
return Math.floor(sum / durations.length);
}