0 of 6 problems solved0%
6 of 6

Total Pages or Minutes

Refine object unions by checking for a property that only one variant has. Use 'pages' in x vs 'duration' in x to decide how to read the value. Reduce the array to a single number for easy comparison.

function metric(x: { pages: number } | { duration: number }) {
  return "pages" in x ? x.pages : x.duration;
}

Sum the metric across the whole list.

Your Task

Sum pages for books and minutes for movies.

  • Define type Book = { title: string; pages: number } and type Movie = { title: string; duration: number }.
  • Define type Media = Book | Movie.
  • Implement function totalWork(items: Media[]): number that adds up pages for books and duration for movies.
  • Keep the function pure and handle the empty array.