Start learning free

LESSON 187 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V2 in JavaScript: Discount and Tax with Integer Math

Practice Read the full explanation

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

You start with:

const item = "Coffee Mug";
const price = 12;
const quantity = 3;
const tax_rate = 8;
const discount = 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 JavaScript "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 = "Coffee Mug";
const price = 12;
const quantity = 3;
const tax_rate = 8;
const discount = 5;

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

STEP 1 OF 10
    Ctrl + Enter to check

    JAVASCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V2 in JavaScript, explained

    Problem overview

    Version 1 printed an item row and a total. Version 2 adds a flat discount and a percentage sales tax. The starter code defines item, price, quantity, tax_rate (a percentage, 8) and discount (an amount, 5). The program prints four lines: subtotal, discount, tax and total.

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

    The subtotal is 12 * 3 = 36. After the discount it is 31. Tax on 31 at 8% is 2.48, rounded down to 2, and the total is 33. The order matters: the discount comes off first, and the tax is charged on the discounted amount.

    Approach

    Each number in the receipt is computed from the previous ones, so the code follows the order of the receipt's logic: subtotal, then after_discount, then tax, then total. Only after all four numbers exist does the program format them into rows.

    The lesson calls this integer math: amounts stay whole. The tax is the only step that can produce a fraction, so it is rounded down with Math.floor, and every later value is a whole number.

    Step by step

    1. Compute subtotal as price * quantity.
    2. Compute after_discount as subtotal - discount. The tax will apply to this value.
    3. Compute tax. Multiply after_discount by tax_rate, divide by 100 and wrap the whole expression in Math.floor. A percent is a number out of 100, so the multiplication comes first and the division by 100 turns it into the share.
    4. Compute total as after_discount + tax.
    5. Build four row strings, line1 to line4, each a label with a dollar sign followed by its number: Subtotal, Discount, Tax and Total.
    6. Join the rows into result with "\n" between each pair. Four rows need three newlines.
    7. Print result with console.log.

    It helps to see why the order of the steps is forced. The tax needs the discounted amount, so after_discount must exist before tax. The total needs the tax, so tax comes before total. The rows can only be written once their numbers are known. Reading the code from top to bottom therefore follows the same path as working the receipt out by hand, and a mistake is easy to locate: print a variable and see which step first gives a wrong number.

    Also notice that the discount is a flat amount of money here, not a percentage. A percentage discount would need its own calculation, like the tax.

    The solution in JavaScript

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

    Notes on the code:

    • The variable names tax_rate and after_discount use underscores, as the starter does. They are valid names, though JavaScript code more often uses camelCase. The lesson keeps the names as they are.
    • item is declared but not used. That is allowed.
    • JavaScript has no separate integer type for ordinary numbers, so 31 * 8 / 100 is 2.48, not 2. The division doesn't truncate by itself, which is why Math.floor is there.
    • Each row string has a string on the left of every +, so numbers are converted to text as they are joined.

    Because the discount is subtracted first, a bigger discount lowers the tax too. With a discount of 20, the amount after the discount is 16, the tax is 1 and the total is 17.

    Tracing the starter data

    The values flow like this: subtotal is 36, after_discount is 31, tax is Math.floor(31 * 8 / 100), that is Math.floor(2.48), which is 2, and total is 33. The four rows are Subtotal: $36, Discount: $5, Tax: $2 and Total: $33.

    Edge cases and common mistakes

    • Taxing the subtotal. Using subtotal in the tax formula gives Math.floor(36 * 8 / 100) = 2 here, which hides the mistake for these numbers. With other values, such as a discount of 20, the answers differ: the tax on 16 is 1, and on 36 it is still 2.
    • Dividing before multiplying. after_discount / 100 * tax_rate is the same in math but not in floating point. With an amount of 116 and a rate of 25, it gives 28.999999999999996, and Math.floor turns that into 28, while 116 * 25 / 100 is exactly 29. Multiplying first keeps the numbers whole until the one division.
    • Forgetting Math.floor. The tax line would read Tax: $2.48 and the total Total: $33.48.
    • Too few newlines. With a missing "\n", two rows run together on one line.
    • A discount larger than the subtotal. The amount after the discount is negative, and so are the tax and the total, because Math.floor rounds toward negative infinity. The lesson's data doesn't cover this case.

    Testing

    • Price 12, quantity 3, rate 8, discount 5 gives 36, 5, 2 and 33.
    • Price 10, quantity 2, rate 10, discount 0 gives subtotal 20, tax 2 and total 22.
    • Price 5, quantity 1, rate 8, discount 0 gives tax 0, because Math.floor(0.4) is 0, and total 5.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free