Start learning free

LESSON 190 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V2 in TypeScript: Tax and Discount

Practice Read the full explanation

Add a discount and sales tax to a single-item purchase using integer math.

You start with:

const item: string = "Coffee Mug";
const price: number = 12;
const quantity: number = 3;
const tax_rate: number = 8;
const discount: number = 5;

Build:

Subtotal: $36
Discount: $5
Tax: $2
Total: $33

Rules:

  • subtotal = price * quantity.
  • after_discount = subtotal - discount (the discount is a flat amount taken off before tax).
  • tax = Math.floor(after_discount * tax_rate / 100) (rounded down with Math.floor). The tax is charged on the discounted amount.
  • total = after_discount + tax.
  • Build four lines (Subtotal, Discount, Tax, Total) and join them with a newline "\n" into one string result, then print it with console.log(result).

Compute the four numbers (subtotal, after_discount, tax, total) before formatting.

In TypeScript "Total: $" + total converts the number to text for you.

Use Math.floor for the whole-number tax: Math.floor(after_discount * tax_rate / 100).

const item: string = "Coffee Mug";
const price: number = 12;
const quantity: number = 3;
const tax_rate: number = 8;
const discount: number = 5;

const subtotal: number = price * quantity;
const after_discount: number = subtotal - discount;
const tax: number = Math.floor(after_discount * tax_rate / 100);
const total: number = after_discount + tax;
const line1: string = "Subtotal: $" + subtotal;
const line2: string = "Discount: $" + discount;
const line3: string = "Tax: $" + tax;
const line4: string = "Total: $" + total;
const result: string = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4;
console.log(result);

STEP 1 OF 10
    Ctrl + Enter to check

    TYPESCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V2 in TypeScript, explained

    Problem overview

    This version adds a flat discount and a sales tax to a one-item purchase. The starter code has item, price, quantity, tax_rate and discount. The tax rate is a whole percent, 8 meaning 8%, and the discount is a flat amount of money taken off before tax.

    The program prints four lines: Subtotal, Discount, Tax and Total. For 3 Coffee Mugs at 12, with a discount of 5 and a tax rate of 8, the output is:

    Subtotal: $36
    Discount: $5
    Tax: $2
    Total: $33

    Approach

    Each figure builds on the one before. The subtotal is the price times the quantity. Taking the discount off gives after_discount. The tax is charged on that discounted amount, and the total is the discounted amount plus the tax. Naming each stage keeps the order of operations visible: discount first, then tax.

    The rules call for integer math, so the tax is rounded down. A percentage is computed as after_discount * tax_rate / 100, multiplying by the rate first and then dividing by 100. In TypeScript every number is a floating-point double and / doesn't truncate: 31 times 8 divided by 100 is 2.48. Math.floor rounds that down to 2. Without it, the same data prints Tax: $2.48 and Total: $33.48.

    The four lines are built as four string variables and joined with newlines into one result, which is printed once.

    For the starter data: the subtotal is 12 times 3, which is 36. After the discount of 5, 31 is left. The tax is 31 times 8 divided by 100, which is 2.48, rounded down to 2. The total is 31 plus 2, which is 33.

    Where the discount sits matters. The rules take it off before the tax, so the tax is charged on 31 and not on 36. Each stage has its own variable, so the order of the statements is the order of the calculation, and a reader can check every figure against the rules.

    The discount is a flat amount of money, not a percentage, so it is subtracted directly. Only the tax uses the multiply-then-divide pattern for percentages.

    The rounding rule is worth a second look. Math.floor always rounds toward smaller numbers, so 2.88 becomes 2 and not 3. With discount = 0 the tax on 36 is 2.88, and the program prints Tax: $2. Rounding is applied once, to the tax, and the other figures are whole numbers already.

    Step by step

    1. Create subtotal: price * quantity, the cost before the discount and the tax.
    2. Create after_discount: subtotal - discount. The discount comes off first.
    3. Create tax: Math.floor(after_discount * tax_rate / 100). Multiply by the rate, divide by 100, and round down so the tax is a whole number.
    4. Create total: after_discount + tax, which is what the customer pays.
    5. Build line1 as "Subtotal: $" followed by subtotal.
    6. Build line2 as "Discount: $" followed by discount.
    7. Build line3 as "Tax: $" followed by tax.
    8. Build line4 as "Total: $" followed by total.
    9. Join into result. Four rows need three "\n" separators, one between each pair.
    10. Print result with console.log.

    The solution in TypeScript

    const item: string = "Coffee Mug";
    const price: number = 12;
    const quantity: number = 3;
    const tax_rate: number = 8;
    const discount: number = 5;
    
    const subtotal: number = price * quantity;
    const after_discount: number = subtotal - discount;
    const tax: number = Math.floor(after_discount * tax_rate / 100);
    const total: number = after_discount + tax;
    const line1: string = "Subtotal: $" + subtotal;
    const line2: string = "Discount: $" + discount;
    const line3: string = "Tax: $" + tax;
    const line4: string = "Total: $" + total;
    const result: string = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4;
    console.log(result);
    

    Language details worth noting:

    • No integer type. number covers both whole numbers and fractions, so the compiler won't flag a fractional tax. Rounding is something the code must do itself, with Math.floor.
    • Underscored names. tax_rate and after_discount are valid identifiers. TypeScript code usually writes camelCase, but the lesson's names are used as given.
    • The same +. Every + in the line expressions has a string on its left, so it joins and turns the number into text.
    • Types are erased. The annotations are checked by the compiler and absent from the JavaScript that runs.

    Edge cases and common mistakes

    • Forgetting Math.floor. The output has Tax: $2.48 and Total: $33.48 instead of whole amounts.
    • Discount of zero. With discount = 0 the output is Subtotal: $36, Discount: $0, Tax: $2 and Total: $38.
    • Tax rate of zero. With tax_rate = 0 the tax is 0 and the total is 31.
    • Small amounts round to nothing. A subtotal of 10 with no discount and a rate of 5 gives 0.5 before rounding, so the tax is 0 and the total is 10.
    • A discount larger than the subtotal. With price 3, quantity 1 and the starter discount of 5, the amount after the discount is -2, and the program prints Tax: $-1 and Total: $-3. Math.floor rounds toward negative infinity, so -0.16 becomes -1. This lesson's rules don't cover that case.
    • Three separators, not four. A trailing "\n" would add an empty line at the end.

    Testing

    • The starter data prints Subtotal: $36, Discount: $5, Tax: $2, Total: $33.
    • With discount = 0 it prints Subtotal: $36, Discount: $0, Tax: $2, Total: $38.
    • With tax_rate = 0 it prints Subtotal: $36, Discount: $5, Tax: $0, Total: $31.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free