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
- Create
subtotal:price * quantity, the cost before the discount and the tax. - Create
after_discount:subtotal - discount. The discount comes off first. - 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. - Create
total:after_discount + tax, which is what the customer pays. - Build
line1as"Subtotal: $"followed bysubtotal. - Build
line2as"Discount: $"followed bydiscount. - Build
line3as"Tax: $"followed bytax. - Build
line4as"Total: $"followed bytotal. - Join into
result. Four rows need three"\n"separators, one between each pair. - Print
resultwithconsole.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.
numbercovers both whole numbers and fractions, so the compiler won't flag a fractional tax. Rounding is something the code must do itself, withMath.floor. - Underscored names.
tax_rateandafter_discountare 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 hasTax: $2.48andTotal: $33.48instead of whole amounts. - Discount of zero. With
discount = 0the output isSubtotal: $36,Discount: $0,Tax: $2andTotal: $38. - Tax rate of zero. With
tax_rate = 0the 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: $-1andTotal: $-3.Math.floorrounds 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 = 0it printsSubtotal: $36,Discount: $0,Tax: $2,Total: $38. - With
tax_rate = 0it printsSubtotal: $36,Discount: $5,Tax: $0,Total: $31.
