Drill

ProblemsTypeScript › billing

Sum the invoice line items

easybillingTypeScript

An invoice lists items with quantity and unit price. Compute the total across all lines.

invoiceLineTotal(items: list<LineItem>) → int

Solve it in the editor →

Where you start

function invoiceLineTotal(items: LineItem[]): number {
  
}

Worked examples

CallResult
invoiceLineTotal([{"name":"Widget","qty":3,"unitPrice":500}])1500
invoiceLineTotal([{"name":"A","qty":2,"unitPrice":1000},{"name":"B","qty":1,"unitPrice":300}])2300
invoiceLineTotal([])0
invoiceLineTotal([{"name":"X","qty":0,"unitPrice":999}])0

Hint

Walk the list, multiply each line, and add.

Reference solution in TypeScript
function invoiceLineTotal(items: LineItem[]): number {
  let total = 0;
  for (const item of items) total += item.qty * item.unitPrice;
  return total;
}

The same problem in another language

More billing problems in TypeScript