Solución
solution.tsTypeScript
interface OrderItem {
price: number;
quantity: number;
}
export function calculateOrderDiscount(a: OrderItem[], b: string): number {
const items = a;
const membership = b;
const MIN_ORDER_QUANTITY = 10;
let total = 0;
// Sumar el total de todos los items del pedido
for (let index = 0; index < items.length; index++) {
total = total + items[index].price * items[index].quantity;
}
// Verificar si el pedido cumple el mínimo requerido
if (total < MIN_ORDER_QUANTITY) return -1;
const discount = getDiscount(membership, total);
return total - discount;
}
function getDiscount(membership: string, total: number){
const GOLD_MEMBERSHIP = "gold";
const GOLD_DISCOUNT_BIg_ORDER = 0.2
const GOLD_DISCOUNT_SMALL_ORDER = 0.1
const SILVER_MEMBERSHIP = "silver";
const SILVER_DISCOUNT_BIg_ORDER = 0.1
const SILVER_DISCOUNT_SMALL_ORDER = 0.05
const BIG_ORDER_VOLUME = 100;
let discount = 0;
// Aplicar descuento según el tipo de membresía y el monto total
if (membership === GOLD_MEMBERSHIP) {
// Pedidos grandes reciben mayor descuento
if (total >= BIG_ORDER_VOLUME) {
discount = total * GOLD_DISCOUNT_BIg_ORDER;
} else {
discount = total * GOLD_DISCOUNT_SMALL_ORDER;
}
} else if (membership === SILVER_MEMBERSHIP) {
// Pedidos grandes reciben mayor descuento
if (total >= BIG_ORDER_VOLUME) {
discount = total * SILVER_DISCOUNT_BIg_ORDER;
} else {
discount = total * SILVER_DISCOUNT_SMALL_ORDER;
}
}
return discount
}0respuestas