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
- Compute
subtotalasprice * quantity. - Compute
after_discountassubtotal - discount. The tax will apply to this value. - Compute
tax. Multiplyafter_discountbytax_rate, divide by 100 and wrap the whole expression inMath.floor. A percent is a number out of 100, so the multiplication comes first and the division by 100 turns it into the share. - Compute
totalasafter_discount + tax. - Build four row strings,
line1toline4, each a label with a dollar sign followed by its number: Subtotal, Discount, Tax and Total. - Join the rows into
resultwith"\n"between each pair. Four rows need three newlines. - Print
resultwithconsole.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_rateandafter_discountuse underscores, as the starter does. They are valid names, though JavaScript code more often uses camelCase. The lesson keeps the names as they are. itemis declared but not used. That is allowed.- JavaScript has no separate integer type for ordinary numbers, so
31 * 8 / 100is 2.48, not 2. The division doesn't truncate by itself, which is whyMath.flooris 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
subtotalin the tax formula givesMath.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_rateis the same in math but not in floating point. With an amount of 116 and a rate of 25, it gives 28.999999999999996, andMath.floorturns that into 28, while116 * 25 / 100is exactly 29. Multiplying first keeps the numbers whole until the one division. - Forgetting
Math.floor. The tax line would readTax: $2.48and the totalTotal: $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.floorrounds 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.
