Calculate customer tier discounts, sum cumulative benefits with a 25% cap, and determine the final amount to pay using if, else if, and else.
JavaScriptBeginnerWith Nexo
condicionales · if-else · reglas-de-negocio
Statement
Your mission
When designing commercial discount rules, it is essential to distinguish between mutually exclusive alternatives (a customer belongs to a single tier) and cumulative benefits (first purchase and coupon bonuses can apply at the same time). Furthermore, when multiple percentage bonuses are cumulative up to a maximum cap, the percentage points are added and capped first, applying the resulting discount only once to the original subtotal.
Implement the solve(input) function, which receives a JSON object with four properties:
subtotal: integer between 0 and 1000000 (in cents).
categoria: string that is exactly "BRONCE", "PLATA", "ORO", or "DIAMANTE".
esPrimeraCompra: boolean.
tieneCupon: boolean.
Discount rules:
Base category discount (descuentoCategoria):
"BRONCE": 0 %
"PLATA": 5 %
"ORO": 10 %
"DIAMANTE": 15 %
Cumulative extra benefits (descuentoAdicional):
If esPrimeraCompra is true, add 5 percentage points.
If tieneCupon is true, add 10 percentage points.
Discount cap (descuentoFinal):
The sum descuentoCategoria + descuentoAdicional, capped at a maximum of 25 % (if the sum exceeds 25, descuentoFinal becomes 25, while keeping descuentoCategoria and descuentoAdicional unchanged).
Return a JSON object with those exact six properties: categoria, descuentoCategoria, descuentoAdicional, descuentoFinal, montoDescuento, and totalPagar.
Rules and constraints
Use explicit conditional structures (if, else if, else) to select the category, accumulate benefits, and apply the cap.
Do not use the ternary operator (? :), configuration objects or maps, loops, arrays, classes, or helper functions.
Do not apply successive discounts one after another to already discounted amounts; sum the percentage points first and apply descuentoFinal once to subtotal.