Start learning free

LESSON 20 OF 348 · CALCULATIONS WITH NUMBERS

Find Discount in TypeScript: Percentage of a Price

Lesson Read the full explanation

We created two variables to store the original price of a product in dollars and the discount percentage applied to that price.

Use these two variables and arithmetic operations to compute and print the discount amount in dollars.


Example:

For example, if price was 150 and discount was 10, the answer would be 15.

Why? Because the discount is 10%. And 10% of 150 dollars is 15 dollars.

STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · CALCULATIONS WITH NUMBERS

    Find Discount in TypeScript, explained

    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

    1. Multiply the price by the percentage: price * discount is 1050 for the starter values. That is 100 times too large, because the percentage was used as a plain number.
    2. Divide by 100 to turn the percentage into a fraction of the price. That gives 10.5.
    3. 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, so price * discount / 100 means 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 * 100 prints exactly that. Computing (price * discount) / 100 with 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 / price asks "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. Use price and discount.

    Testing

    Try the formula by hand with simple numbers before running it:

    • price = 150, discount = 10 gives 15.
    • price = 200, discount = 50 gives 100.
    • price = 80, discount = 0 gives 0.
    • price = 70, discount = 15 gives 10.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 number keeps the decimal part, so results like 10.5 are normal.
    • Read the task for what to print: the discount amount and the discounted price are different numbers.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free