0 of 6 problems solved0%
6 of 6

Sum Prices Safely

Arrays often include items with missing numbers. A price may be undefined or null. Sum only the numbers and treat missing as 0.

const prices = [1, undefined, 3, null];
const total = prices.reduce((acc, p) => acc + (p ?? 0), 0);

Keep the function pure and return a number result.

Your Task

Add up prices, skipping missing values.

  • Define interface Item { name: string; price?: number | null }.
  • Write function totalPrice(items: Item[]): number.
  • Treat null and undefined prices as 0.
  • Do not mutate the input array or its items.
  • Return 0 for an empty list.