Drill

ProblemsTypeScript › events

How many openings in the coming weeks

mediumeventsTypeScript

A recurring weekly pattern marks which of the seven weekdays are open. Count the total openings over a number of full weeks.

weeklyOpenings(pattern: list<bool>, weeks: int) → int

Solve it in the editor →

Where you start

function weeklyOpenings(pattern: boolean[], weeks: number): number {
  
}

Worked examples

CallResult
weeklyOpenings([true,false,true,false,true,false,false], 4)12
weeklyOpenings([false,false,false,false,false,false,false], 10)0
weeklyOpenings([true,true,true,true,true,true,true], 2)14
weeklyOpenings([true,false,false,false,false,false,false], 0)0

Hint

Count the true entries in the pattern once, then multiply.

Reference solution in TypeScript
function weeklyOpenings(pattern: boolean[], weeks: number): number {
  let perWeek = 0;
  for (const d of pattern) if (d) perWeek++;
  return perWeek * weeks;
}

The same problem in another language

More events problems in TypeScript