Solución

@isakidev·29/10/2026TypeScript
solution.tsTypeScript
interface OrderItem {
  price: number;
  quantity: number;
}

type DiscountCategory = 'gold' | 'silver';

const MIN_ORDER_AMOUNT = 10;
const HIGH_ORDER_THRESHOLD = 100;

const GOLD_HIGH_RATE = 0.20
const GOLD_STANDARD_RATE = 0.10
const SILVER_HIGH_RATE = 0.10
const SILVER_STANDARD_RATE = 0.05


const DISCOUNT_RATES = {
  gold: {
    high: GOLD_HIGH_RATE,
    standard: GOLD_STANDARD_RATE,
  },
  silver: {
    high: SILVER_HIGH_RATE,
    standard: SILVER_STANDARD_RATE,
  },
} as const satisfies Record<
  DiscountCategory,
  { high: number; standard: number }
>;

const ERROR_VALUE = -1

function getTotalPrice(items: OrderItem[]): number {
  return items.reduce(
    (acc, current) => acc + current.price * current.quantity,
    0,
  );
}

function getDiscountRate(total: number, discountCategory: DiscountCategory): number {
  const rates = DISCOUNT_RATES[discountCategory];

  if (!rates) return 0
  
  return total >= HIGH_ORDER_THRESHOLD ? rates.high : rates.standard;
}

export function calculateOrderDiscount(
  items: OrderItem[],
  discountCategory: DiscountCategory,
): number {

  const total = getTotalPrice(items)

  if (total < MIN_ORDER_AMOUNT) return ERROR_VALUE

  const discount = getDiscountRate(total, discountCategory)

  return total - total * discount;
}
0respuestas
Respuestas

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.