Problems › TypeScript › billing
Sum the invoice line items
An invoice lists items with quantity and unit price. Compute the total across all lines.
- Each line contributes qty multiplied by unitPrice.
- An empty list totals zero.
invoiceLineTotal(items: list<LineItem>) → int
Where you start
function invoiceLineTotal(items: LineItem[]): number {
}
Worked examples
| Call | Result |
|---|---|
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;
}