Solución
solution.tsTypeScript
interface OrderItem {
price: number;
quantity: number;
}
function calculateTotalOrder(order: OrderItem[]) {
return order.reduce((previous, current) => previous + current.price * current.quantity, 0)
}
function calculateDiscount(total: number, memberShip: string): number {
const MIN_DISCOUNT_TOTAL = 100
if (!["gold", "silver"].includes(memberShip)) return total;
switch(memberShip) {
case "gold": {
const GOLD_MIN_DISCOUNT = 0.9, GOLD_MAX_DISCOUNT = 0.8
return total * (total < MIN_DISCOUNT_TOTAL ? GOLD_MIN_DISCOUNT : GOLD_MAX_DISCOUNT);
}
case "silver": {
const SILVER_MIN_DISCOUNT = 0.95, SILVER_MAX_DISCOUNT = 0.9
return total * (total < MIN_DISCOUNT_TOTAL ? SILVER_MIN_DISCOUNT : SILVER_MAX_DISCOUNT);
}
default: return total;
}
}
export function calculateOrderDiscount(order: OrderItem[], memberShip: string): number {
const MIN_ORDER_TOTAL = 10, NO_RESULT = -1
let total = calculateTotalOrder(order)
if (total < MIN_ORDER_TOTAL) return NO_RESULT
return calculateDiscount(total, memberShip);
}0respuestas