Drill

ProblemsTypeScript › dates

Count the working days in a span

mediumdatesTypeScript

An SLA clock only runs on working days, so a span of calendar days has to be reduced to the ones that count.

businessDays(startWeekday: int, spanDays: int) → int

Solve it in the editor →

Where you start

function businessDays(startWeekday: number, spanDays: number): number {
  
}

Worked examples

CallResult
businessDays(0, 7)5
businessDays(0, 5)5
businessDays(5, 2)0
businessDays(4, 4)2

Hint

Walk the days and take the weekday modulo 7, or work out the whole weeks first and handle the remainder.

Reference solution in TypeScript
function businessDays(startWeekday: number, spanDays: number): number {
  if (spanDays <= 0 || startWeekday < 0 || startWeekday > 6) return 0;
  let n = 0;
  for (let i = 0; i < spanDays; i++) {
    if ((startWeekday + i) % 7 < 5) n++;
  }
  return n;
}

The same problem in another language

More dates problems in TypeScript