Solución

@estouchedisindo·19/5/2026TypeScript
solution.tsTypeScript
interface OrderItem {
	price: number;
	quantity: number;
}

type MemberType = 'gold' | 'silver';

interface Members {
	memberType: MemberType;
	largeOrderDiscount: number;
	regularDiscount: number;
}

const MINIMUM_LARGE_ORDER = 100;
const MINIMUM_ORDER_PRICE = 10;
const NOT_VALID = -1;
// 
const GOLD_BIG_DISCOUNT = 0.20;
const GOLD_REGULAR_DISCOUNT = 0.10;
const SILVER_BIG_DISCOUNT = 0.10;
const SILVER_REGULAR_DISCOUNT = 0.05;
// 
const GOLD_MEMBER: Members = {
	memberType: 'gold',
	largeOrderDiscount: GOLD_BIG_DISCOUNT,
	regularDiscount: GOLD_REGULAR_DISCOUNT
};
const SILVER_MEMBER: Members = {
	memberType: 'silver',
	largeOrderDiscount: SILVER_BIG_DISCOUNT,
	regularDiscount: SILVER_REGULAR_DISCOUNT,
};

// FUNCIONES AUXILIARES
function getTotalAmount(orderedItem: OrderItem[]): number {
	// Sumar el total de todos los items del pedido
	let total = 0;

	for (let iteration = 0; iteration < orderedItem.length; iteration++) {
		
		total = total + orderedItem[iteration].price * orderedItem[iteration].quantity;
	}

	return total;
}

function getDiscount(total: number, memberType: string): number {
	let discount = 0;

	// Aplicar descuento según el tipo de membresía y el monto total
	if (memberType === 'gold') {
		// Pedidos grandes reciben mayor descuento
		if (total >= MINIMUM_LARGE_ORDER) {
			discount = total * GOLD_MEMBER.largeOrderDiscount;
		} else {
			discount = total * GOLD_MEMBER.regularDiscount;
		}
	} else if (memberType === 'silver') {
		// Pedidos grandes reciben mayor descuento
		if (total >= MINIMUM_LARGE_ORDER) {
			discount = total * SILVER_MEMBER.largeOrderDiscount;
		} else {
			discount = total * SILVER_MEMBER.regularDiscount;
		}
	}

	return discount;
}

export function calculateOrderDiscount(
	orderedItem: OrderItem[],
	memberType: string,
): number {
	// Sumar el total de todos los items del pedido
	const total = getTotalAmount(orderedItem);

	// Verificar si el pedido cumple el mínimo requerido
	if (total < MINIMUM_ORDER_PRICE) return NOT_VALID;

	// Aplicar descuento según el tipo de membresía y el monto total
	const discount = getDiscount(total, memberType);

	return total - discount;
}
0respuestas
Respuestas

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.