Solución
solution.tsTypeScript
type TypeProducts = 'electronics' | 'clothing' | 'food'
const VALUE_ZERO = 0
const PERCENT_TAX = 1.21
const ELECTRONIC_DISCOUNT = 0.15
const CLOTHING_DISCOUNT = 0.20
const FOOD_DISCOUNT = 0.05
function calcDiscount(price: number, category: TypeProducts) {
const mapDiscount = {
electronics: price * ELECTRONIC_DISCOUNT,
clothing: price * CLOTHING_DISCOUNT,
food: price * FOOD_DISCOUNT,
}
return mapDiscount[category] ?? 0
}
export function getProductFinalPrice(price: number, category: TypeProducts, stock: number): number {
// Verificar si el stock es válido
if (stock <= VALUE_ZERO) return -1;
// Verificar si el precio es válido
if (price <= VALUE_ZERO) return -1;
let discount = calcDiscount(price, category);
// Calcular el precio con descuento aplicado
let discounted = price - discount;
// Aplicar el impuesto sobre el precio con descuento
let result = discounted * PERCENT_TAX;
// Redondear a dos decimales
return Math.round(result * 100) / 100;
}0respuestas