Solución

@megatek·1/5/2026TypeScript
solution.tsTypeScript
interface OrderItem {
  price: number;
  quantity: number;
}

const VALOR_PEDIDO_GRANDE = 100;
const GOLD_DESCUENTO_PEDIDO_GRANDE = 0.20;
const GOLD_DESCUENTO_PEDIDO_NORMAL = 0.10;
const SILVER_DESCUENTO_PEDIDO_GRANDE = 0.10;
const SILVER_DESCUENTO_PEDIDO_NORMAL = 0.05;
const VALOR_NETO_MINIMO = 10;

const casos: Record<string, (total: number) => number> = {
  "gold": (total) => {
    // Pedidos grandes reciben mayor descuento
    if (total >= VALOR_PEDIDO_GRANDE) {
      return total * GOLD_DESCUENTO_PEDIDO_GRANDE;
    } else {
      return total * GOLD_DESCUENTO_PEDIDO_NORMAL;
    }
  },
  "silver": (total) => {
    // Pedidos grandes reciben mayor descuento
    if (total >= VALOR_PEDIDO_GRANDE) {
      return total * SILVER_DESCUENTO_PEDIDO_GRANDE;
    } else {
      return total * SILVER_DESCUENTO_PEDIDO_NORMAL;
    }
  }
}

function aplicarReglasMembresia(membresia: string, total: number) {
  switch (membresia) {
    case "gold":
      return casos["gold"](total);
    case "silver":
      return casos["silver"](total);
    default:
      return 0;
  }
}

function netoItemsPedido(elementosDelPedido: OrderItem[]): number {
  // Sumar el total de todos los items del pedido
  let acumulador = 0;
  for (const elementoPedido of elementosDelPedido) {
    acumulador = acumulador + elementoPedido.price * elementoPedido.quantity;
  }
  return acumulador;
}

function calcularDescuento(membresia: string, total: number) {
  let descuento = aplicarReglasMembresia(membresia, total);
  return descuento;
}

export function calculateOrderDiscount(elementosDelPedido: OrderItem[], membresia: string): number {
  const VALOR_NETO = netoItemsPedido(elementosDelPedido);
  // Verificar si el pedido cumple el mínimo requerido
  if (VALOR_NETO < VALOR_NETO_MINIMO) return -1;
  const DESCUENTO = calcularDescuento(membresia, VALOR_NETO)
  return VALOR_NETO - DESCUENTO;
}
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.