Calculate a purchase breakdown in integer cents by applying a percentage discount with Math.floor, adding shipping, and checking if the available balance is sufficient.
JavaScriptBeginnerWith Nexo
operadores · expresiones · aritmetica
Statement
Your mission
In e-commerce, it is common practice to work with amounts in integer cents to avoid decimal inaccuracies and apply business rules in the exact order required: first the product subtotal, then the percentage discount on those products, and finally the shipping cost.
Implement the solve(input) function, which receives a JSON object with five integer properties:
precioUnitario: integer between 0 and 100000 (in cents).
cantidad: integer between 0 and 100 (units).
descuentoPorcentaje: integer between 0 and 100 (discount percentage).
costoEnvio: integer between 0 and 50000 (in cents).
saldoDisponible: integer between 0 and 20000000 (in cents).
Perform the following calculations in order:
subtotal: the product precioUnitario * cantidad.
descuentoAplicado: the discount applied only to the subtotal, calculated as Math.floor(subtotal * descuentoPorcentaje / 100) (rounding down the discounted amount).
totalProductos: the subtraction subtotal - descuentoAplicado.
totalFinal: the sum totalProductos + costoEnvio.
saldoRestante: the difference saldoDisponible - totalFinal (can be negative if the balance is insufficient).
puedeComprar: the boolean result of evaluating whether saldoDisponible >= totalFinal.
Return a JSON object with those exact six properties: subtotal, descuentoAplicado, totalProductos, totalFinal, saldoRestante, and puedeComprar.
Rules and constraints
Do not use if, else, switch, the ternary operator (? :), loops, arrays, classes, or helper functions.
All monetary amounts remain expressed in integer cents.
The discount never affects the shipping cost, and downward rounding is applied to the discounted amount rather than the total.