Solución
solution.tsTypeScript
interface OrderItem {
price: number;
quantity: number;
}
const PERCENT_20 = 0.20
const PERCENT_10 = 0.10
const PERCENT_05 = 0.05
const MIN_COST_REQUIRED = 10
const TOP_LIMIT = 100
const MEMBERSHIP_GOLD = "gold"
const MEMBERSHIP_SILVER = "silver"
function applyDiscount(membership: string, total: number) {
if (membership === MEMBERSHIP_GOLD) {
// Pedidos grandes reciben mayor descuento
if (total >= TOP_LIMIT) {
return total * PERCENT_20;
}
return total * PERCENT_10;
}
if (membership === MEMBERSHIP_SILVER) {
// Pedidos grandes reciben mayor descuento
if (total >= TOP_LIMIT) {
return total * PERCENT_10;
}
return total * PERCENT_05;
}
return 0;
}
function calculateTotal(orders: OrderItem[]) {
let total = 0;
for (let idxOrder = 0; idxOrder < orders.length; idxOrder++) {
total = total + orders[idxOrder].price * orders[idxOrder].quantity;
}
return total
}
export function calculateOrderDiscount(orders: OrderItem[], membership: string): number {
let total = calculateTotal(orders);
// Verificar si el pedido cumple el mínimo requerido
if (total < MIN_COST_REQUIRED) return -1;
// Calcula el descuento
const discount = applyDiscount(membership, total)
return total - discount;
}0respuestas