Solución
solution.tsTypeScript
interface OrderItem {
price: number;
quantity: number;
}
enum MembershipTypes {
'gold',
'silver'
}
const INIT_VALUE = 0;
const GOLD_HIGH_DISCOUNT = 0.20;
const GOLD_LOW_DISCOUNT = 0.10;
const SILVER_HIGH_DISCOUNT = 0.10;
const SILVER_LOW_DISCOUNT = 0.05;
const LARGE_ORDER_THRESHOLD = 100;
const MIN_ORDER_TOTAL = 10;
const INVALID_ORDER = -1;
export function calculateTotal(items: OrderItem[]) : number {
let total = INIT_VALUE;
for (let item = INIT_VALUE; item < items.length; item++) {
total = total + items[item].price * items[item].quantity;
}
return total;
}
export function calculateOrderDiscount(items: OrderItem[], membershipType: string): number {
let discount = INIT_VALUE;
let total = calculateTotal(items);
if (total < MIN_ORDER_TOTAL) return INVALID_ORDER;
if (membershipType === MembershipTypes[0]) {
if (total >= LARGE_ORDER_THRESHOLD) {
discount = total * GOLD_HIGH_DISCOUNT;
} else {
discount = total * GOLD_LOW_DISCOUNT;
}
} else if (membershipType === MembershipTypes[1]) {
if (total >= LARGE_ORDER_THRESHOLD) {
discount = total * SILVER_HIGH_DISCOUNT;
} else {
discount = total * SILVER_LOW_DISCOUNT;
}
}
return total - discount;
}0respuestas