Start learning free

LESSON 21 OF 410 · CALCULATIONS WITH NUMBERS

Exercise: Price After Discount in JavaScript

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 price after the discount.


Example:

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

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

STEP 1 OF 1
    Ctrl + Enter to check

    JAVASCRIPT · CALCULATIONS WITH NUMBERS

    Price After Discount in JavaScript, explained

    Understanding the Problem

    The core challenge of this problem is to correctly apply a percentage discount to a given price. This is a common task in e-commerce and retail applications where discounts are frequently applied to products.

    Potential pitfalls include incorrect calculations due to misunderstanding percentage operations or arithmetic errors.

    Approach

    To solve this problem, we need to follow these steps:

    1. Calculate the discount amount by multiplying the original price by the discount percentage divided by 100.
    2. Subtract the discount amount from the original price to get the final price after the discount.

    Let's break down the steps:

    1. Discount Amount = (Price * Discount) / 100
    2. Final Price = Price - Discount Amount

    Algorithm

    Here is a step-by-step breakdown of the algorithm:

    1. Read the original price and discount percentage.
    2. Calculate the discount amount using the formula: discountAmount = (price * discount) / 100.
    3. Subtract the discount amount from the original price to get the final price: finalPrice = price - discountAmount.
    4. Print the final price.

    Code Implementation

    // Define the original price and discount percentage
    let price = 150; // Example price
    let discount = 10; // Example discount percentage
    
    // Calculate the discount amount
    let discountAmount = (price * discount) / 100;
    
    // Calculate the final price after discount
    let finalPrice = price - discountAmount;
    
    // Print the final price
    console.log(finalPrice); // Output: 135
    

    Complexity Analysis

    The time complexity of this solution is O(1) because the calculations involve a constant number of operations regardless of the input values. The space complexity is also O(1) as we are using a fixed amount of extra space for variables.

    Edge Cases

    Consider the following edge cases:

    • If the discount is 0%, the final price should be the same as the original price.
    • If the discount is 100%, the final price should be 0.
    • If the price is 0, the final price should also be 0 regardless of the discount percentage.

    Examples:

    • Price: 200, Discount: 0% -> Final Price: 200
    • Price: 200, Discount: 100% -> Final Price: 0
    • Price: 0, Discount: 50% -> Final Price: 0

    Testing

    To test the solution comprehensively, consider the following test cases:

    • Simple cases with non-zero price and discount.
    • Edge cases as discussed above.
    • Cases with large values to ensure no overflow or precision issues.

    Example test cases:

    // Test case 1: Simple case
    let price1 = 150;
    let discount1 = 10;
    console.log((price1 - (price1 * discount1) / 100)); // Expected output: 135
    
    // Test case 2: No discount
    let price2 = 200;
    let discount2 = 0;
    console.log((price2 - (price2 * discount2) / 100)); // Expected output: 200
    
    // Test case 3: Full discount
    let price3 = 200;
    let discount3 = 100;
    console.log((price3 - (price3 * discount3) / 100)); // Expected output: 0
    
    // Test case 4: Zero price
    let price4 = 0;
    let discount4 = 50;
    console.log((price4 - (price4 * discount4) / 100)); // Expected output: 0
    

    Thinking and Problem-Solving Tips

    When approaching such problems, it is essential to:

    • Understand the mathematical operations involved.
    • Break down the problem into smaller, manageable steps.
    • Consider edge cases and test your solution against them.
    • Practice similar problems to improve your problem-solving skills.

    Conclusion

    In this blog post, we discussed how to calculate the price after applying a discount. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.

    Keep practicing and exploring further to enhance your skills!

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free