Solución
solution.tsTypeScript
interface OrderItem {
price: number;
quantity: number;
}
type Membership = 'gold' | 'silver';
const GOLD_BIG_DISCOUNT = 0.20;
const GOLD_SMALL_DISCOUNT = 0.10;
const SILVER_BIG_DISCOUNT = 0.10;
const SILVER_SMALL_DISCOUNT = 0.05;
const BIG_ORDER = 100;
function calculateTotal(items: OrderItem[]) {
return items.reduce((prev, curr) => (curr.price * curr.quantity) + prev , 0)
}
function calculateDiscount(membership: Membership, total: number) {
if( membership === 'gold') {
if ( total >= BIG_ORDER ) return total * GOLD_BIG_DISCOUNT
return total * GOLD_SMALL_DISCOUNT;
}
if ( membership === 'silver') {
if ( total >= BIG_ORDER ) return total * SILVER_BIG_DISCOUNT;
return total * SILVER_SMALL_DISCOUNT;
}
return 0;
}
export function calculateOrderDiscount(items: OrderItem[], membership: Membership): number {
const total = calculateTotal(items)
// Verificar si el pedido cumple el mínimo requerido
if (total < 10) return -1;
const discount = calculateDiscount(membership, total)
return total - discount;
}0respuestas