Problem: Apply Flexible Discounts for Marketing Campaigns

Easy
15 min
Build a discount calculator that applies different discount rules—flat, percentage, or loyalty—based on configuration, without changing existing code.

Problem statement

You’re building a checkout system for an online store. The discount logic keeps changing: sometimes marketing runs a flat discount, other times a percentage discount, and for loyal customers, there’s a tiered loyalty discount.

Discount logic often evolves into lengthy chains of conditionals within checkout code. Each new rule means touching stable logic and risking regressions. You’ve decided to apply the Strategy Pattern so that each discount rule can live independently and be swapped in at runtime via configuration.

Goal

Implement both components of the pattern:

  1. Strategies:

    1. Create FlatDiscountPercentageDiscount, and LoyaltyDiscount classes.

    2. Each must define the .calculate(amount) method that returns the final price after applying its rule.

  2. Context:

    1. Implement DiscountService that uses one discount strategy at a time.

    2. It must delegate computation to the active strategy’s .calculate() method.

    3. Allow strategy changes through .setStrategy().

Constraints

  • No if or switch logic inside DiscountService.

  • Each discount strategy must be self-contained—no shared helper functions.

  • Adding a new discount type should not require modifying existing ones.

Sample output

The examples below illustrate what the output should look like:

const subtotal = 100;
const service = new DiscountService(new FlatDiscount());
console.log('Flat:', service.apply(subtotal)); // e.g., 90
/* Expected output:
Flat: 90
*/
service.setStrategy(new PercentageDiscount());
console.log('Percentage:', service.apply(subtotal)); // e.g., 80
/* Expected output:
Percentage: 80
*/
service.setStrategy(new LoyaltyDiscount());
console.log('Loyalty:', service.apply(subtotal)); // e.g., 85
/* Expected output:
Loyalty: 85
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Problem: Apply Flexible Discounts for Marketing Campaigns

Easy
15 min
Build a discount calculator that applies different discount rules—flat, percentage, or loyalty—based on configuration, without changing existing code.

Problem statement

You’re building a checkout system for an online store. The discount logic keeps changing: sometimes marketing runs a flat discount, other times a percentage discount, and for loyal customers, there’s a tiered loyalty discount.

Discount logic often evolves into lengthy chains of conditionals within checkout code. Each new rule means touching stable logic and risking regressions. You’ve decided to apply the Strategy Pattern so that each discount rule can live independently and be swapped in at runtime via configuration.

Goal

Implement both components of the pattern:

  1. Strategies:

    1. Create FlatDiscountPercentageDiscount, and LoyaltyDiscount classes.

    2. Each must define the .calculate(amount) method that returns the final price after applying its rule.

  2. Context:

    1. Implement DiscountService that uses one discount strategy at a time.

    2. It must delegate computation to the active strategy’s .calculate() method.

    3. Allow strategy changes through .setStrategy().

Constraints

  • No if or switch logic inside DiscountService.

  • Each discount strategy must be self-contained—no shared helper functions.

  • Adding a new discount type should not require modifying existing ones.

Sample output

The examples below illustrate what the output should look like:

const subtotal = 100;
const service = new DiscountService(new FlatDiscount());
console.log('Flat:', service.apply(subtotal)); // e.g., 90
/* Expected output:
Flat: 90
*/
service.setStrategy(new PercentageDiscount());
console.log('Percentage:', service.apply(subtotal)); // e.g., 80
/* Expected output:
Percentage: 80
*/
service.setStrategy(new LoyaltyDiscount());
console.log('Loyalty:', service.apply(subtotal)); // e.g., 85
/* Expected output:
Loyalty: 85
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node.js
// --- Strategies (implement these) ---
class FlatDiscount {}
class PercentageDiscount {}
class LoyaltyDiscount {}
// --- Context (implement this too) ---
class DiscountService {
constructor(strategy) {}
setStrategy(strategy) {}
apply(amount) {}
}
// Example usage
const subtotal = 100;
const service = new DiscountService(new FlatDiscount());
console.log('Flat:', service.apply(subtotal)); // e.g., 90
/* Expected output:
Flat: 90
*/
service.setStrategy(new PercentageDiscount());
console.log('Percentage:', service.apply(subtotal)); // e.g., 80
/* Expected output:
Percentage: 80
*/
service.setStrategy(new LoyaltyDiscount());
console.log('Loyalty:', service.apply(subtotal)); // e.g., 85
/* Expected output:
Loyalty: 85
*/