Drill

ProblemsJavaScript › events

How many active weekdays in a span

mediumeventsJavaScript

A project runs for a number of calendar days but only counts effort on specified weekdays. Return the number of active days.

activeDayCount(activeDays: list<int>, days: int) → int

Solve it in the editor →

Where you start

function activeDayCount(activeDays, days) {
  
}

Worked examples

CallResult
activeDayCount([0], 7)1
activeDayCount([0,2,4], 7)3
activeDayCount([0,2,4], 10)5
activeDayCount([3], 1)0

Hint

Build a 7-slot lookup array of booleans once, then loop over the days and test each weekday against it.

Reference solution in JavaScript
function activeDayCount(activeDays, days) {
  const active = [false, false, false, false, false, false, false];
  for (const d of activeDays) active[d] = true;
  let count = 0;
  for (let i = 0; i < days; i++) if (active[i % 7]) count++;
  return count;
}

The same problem in another language

More events problems in JavaScript