Solución
JL@juanluisabreu_4c541ef6
·2/4/2026TypeScriptsolution.tsTypeScript
interface OrderItem {
price: number;
quantity: number;
}
function calculateSubtotal(total: number, discount: number) {
return total-discount;
}
export function calculateOrderDiscount(orderItem: OrderItem[], member: string): number {
let total = 0;
let discount = 0;
const MIM_VALUE_ORDER = 10;
const MIM_VALUE_RETURN = -1;
const GOLD_MEMBER_TYPE = "gold";
const BIG_ORDER_LIMIT = 100;
const GOLD_MAX_DISCOUNT = 0.20;
const GOLD_MIN_DISCOUNT = 0.10;
const SILVER_MEMBER_TYPE = "silver";
const SILVER_MAX_DISCOUNT = 0.10;
const SILVER_MIN_DISCOUNT = 0.05;
// Sumar el total de todos los items del pedido
for (let indice = 0; indice < orderItem.length; indice++) {
total = total + orderItem[indice].price * orderItem[indice].quantity;
}
// Verificar si el pedido cumple el mínimo requerido
if (total < MIM_VALUE_ORDER) return MIM_VALUE_RETURN;
// Aplicar descuento según el tipo de membresía y el monto total
if (member === GOLD_MEMBER_TYPE) {
// Pedidos grandes reciben mayor descuento
if (total >= BIG_ORDER_LIMIT) {
discount = total * GOLD_MAX_DISCOUNT;
} else {
discount = total * GOLD_MIN_DISCOUNT;
}
} else if (member === SILVER_MEMBER_TYPE) {
// Pedidos grandes reciben mayor descuento
if (total >= BIG_ORDER_LIMIT) {
discount = total * SILVER_MAX_DISCOUNT;
} else {
discount = total * SILVER_MIN_DISCOUNT;
}
}
return calculateSubtotal(total,discount);
}0respuestas