Your mission
In the checkout calculation engine of Nexo's digital store, each financial rule (subtotal, discount, tax on the discounted amount, final total with shipping, and summary line) lives in its own reusable function. Instead of sharing global variables, functions collaborate with one another: the return value of one function becomes the argument to the next.
You must implement these five helper functions:
calcularSubtotal(precio, cantidad): returns precio * cantidad.
calcularDescuento(subtotal, porcentaje): returns Math.floor((subtotal * porcentaje) / 100).
calcularImpuesto(baseImponible, porcentaje): returns Math.floor((baseImponible * porcentaje) / 100).
calcularTotal(totalProductos, impuesto, envio): returns totalProductos + impuesto + envio.
generarResumen(subtotal, descuento, impuesto, envio, total): returns the exact string "Subtotal: X | Descuento: Y | Impuesto: Z | Envío: W | Total: T", replacing X, Y, Z, W, and T with their integer cent values (without currency symbols or thousands separators).
Additionally, implement solve(input), which receives a JSON object with five integer properties:
precioUnitario: integer between 0 and 100000 (in cents).
cantidad: integer between 0 and 100.
descuentoPorcentaje: integer between 0 and 50.
impuestoPorcentaje: integer between 0 and 25.
costoEnvio: integer between 0 and 50000 (in cents).
Flow rules in solve(input):
subtotal = calcularSubtotal(precioUnitario, cantidad)
descuento = calcularDescuento(subtotal, descuentoPorcentaje)
totalProductos = subtotal - descuento
impuesto = calcularImpuesto(totalProductos, impuestoPorcentaje) (tax is calculated on totalProductos, never on the original subtotal or shipping)
totalFinal = calcularTotal(totalProductos, impuesto, costoEnvio) (shipping receives neither discounts nor taxes)
resumen = generarResumen(subtotal, descuento, impuesto, costoEnvio, totalFinal)
Return a JSON object with those exact six properties: subtotal, descuento, totalProductos, impuesto, totalFinal, and resumen.
Rules and constraints
- Implement and use all five helper functions, returning their results with
return.
- Do not use global variables,
console.log as a return mechanism, classes, arrays, recursion, or asynchronous functions.