Problem overview
The starter code stores a product's original price in dollars and a discount percentage. The task is to print the discount amount in dollars, not the new price.
let price: number = 70;
let discount: number = 15;
The example in the task uses a price of 150 and a discount of 10. A 10% discount on 150 dollars is 15 dollars, so that is the answer. With the starter values, 15% of 70 dollars is 10.5.
Thinking about percentages
A percentage is a fraction of 100. "15 percent" means 15 out of every 100, or 15 / 100. Taking a percentage of a price means multiplying the price by that fraction:
discount amount = price * discount / 100
Check it against the example: 150 * 10 / 100 is 1500 / 100, which is 15. The task's wording says that discount holds a percentage, so you can't print discount itself (15 would be right only when the price is 100) and you can't print price * discount (that is 1050 dollars).
Step by step
- Multiply the price by the percentage:
price * discountis 1050 for the starter values. That is 100 times too large, because the percentage was used as a plain number. - Divide by 100 to turn the percentage into a fraction of the price. That gives 10.5.
- Print the result with
console.log.
The lesson's solution groups the multiplication in parentheses before dividing:
let price: number = 70;
let discount: number = 15;
console.log((price * discount) / 100);
It prints:
10.5
Notes about the division
- The parentheses are optional here.
*and/have the same precedence and run from left to right, soprice * discount / 100means the same thing. The parentheses make the order clear to a reader. - The order matters for the reader, not for the result. Written as
price * (discount / 100), the code gives the same 10.5 for these values. Multiplying first is usually preferred, since the product of whole numbers is exact and only the final division can produce a fraction. - Decimals are kept. There is no integer division in TypeScript: all numbers are doubles, and
/keeps the fractional part. If the result were truncated, 10.5 would become 10, which would be wrong. - Rounding errors can appear. Some prices and percentages give results like
7.000000000000001. For example,0.07 * 100prints exactly that. Computing(price * discount) / 100with whole numbers avoids the problem in this exercise.
Common mistakes
- Printing the new price instead of the discount. The price after a 15% discount on 70 dollars is 59.5. The task asks for the amount that was taken off, 10.5.
- Forgetting to divide by 100. The output 1050 is a sign of this. A discount can never be larger than the price, so a large output should make you check your expression.
- Dividing the other way.
discount / priceasks "what fraction of the price is the percentage?", which is meaningless here. - Copying the numbers. Writing
console.log(10.5)would match this one case but ignores the variables. Usepriceanddiscount.
Testing
Try the formula by hand with simple numbers before running it:
price = 150,discount = 10gives15.price = 200,discount = 50gives100.price = 80,discount = 0gives0.price = 70,discount = 15gives10.5.
To test the code itself, change the two numbers at the top of the editor and run it again. The formula must work for every pair of values, not only the ones the task gave you, and an answer that is correct for several different pairs is much more convincing than one that is correct for a single pair.
Key takeaways
- A percentage of a value is
value * percent / 100. - Division on
numberkeeps the decimal part, so results like10.5are normal. - Read the task for what to print: the discount amount and the discounted price are different numbers.