Example 1
Input
{
"cantidad": 3,
"precioUnitario": 1000,
"dineroDisponible": 3000,
"descuentoPorcentaje": 10
}Expected result
{
"total": 2700,
"cambio": 300,
"subtotal": 3000,
"descuento": 300,
"puedeComprar": true
}Nexo is preparing the challenges…
Nexo · Connect the ideas
Investigate five logic bugs in the Código con Criterio store checkout calculator with Nexo and fix the subtotal, discount, total, purchase eligibility, and change calculations.
javascript · debugging · arithmetic · comparisons · conditionals
At the Código con Criterio tech store, several students reported wrong charges and negative change when trying to purchase equipment. Nexo inspected the checkout module and discovered five logic bugs in the solve(input) function.
The function receives an input object with four integers:
precioUnitario: integer from 0 to 100000 (in cents).cantidad: integer from 0 to 100.descuentoPorcentaje: integer from 0 to 50.dineroDisponible: integer from 0 to 10000000 (in cents).subtotal): product of precioUnitario * cantidad.descuento): integer discount rounded down via Math.floor((subtotal * descuentoPorcentaje) / 100).total): result of subtracting descuento from subtotal (subtotal - descuento).puedeComprar): true if dineroDisponible >= total (including when available money matches the exact total), or false otherwise.cambio): if puedeComprar is true, return dineroDisponible - total; if puedeComprar is false, return strictly 0.Inspect the buggy starter code, identify all five logic bugs, and fix them while keeping the exact function signature and the five return object properties: subtotal, descuento, total, puedeComprar, and cambio.
Input
{
"cantidad": 3,
"precioUnitario": 1000,
"dineroDisponible": 3000,
"descuentoPorcentaje": 10
}Expected result
{
"total": 2700,
"cambio": 300,
"subtotal": 3000,
"descuento": 300,
"puedeComprar": true
}Input
{
"cantidad": 2,
"precioUnitario": 2000,
"dineroDisponible": 3000,
"descuentoPorcentaje": 25
}Expected result
{
"total": 3000,
"cambio": 0,
"subtotal": 4000,
"descuento": 1000,
"puedeComprar": true
}Input
{
"cantidad": 4,
"precioUnitario": 1500,
"dineroDisponible": 5000,
"descuentoPorcentaje": 0
}Expected result
{
"total": 6000,
"cambio": 0,
"subtotal": 6000,
"descuento": 0,
"puedeComprar": false
}Implement solve(input) and return the result. Synchronous JavaScript, without packages, network or account access.
477/10.000 characters · In the advanced editor, press Escape and then Tab to leave.
Your code is restored in this browser. Saving to your account is optional.
Try solving it first. Viewing the solution records assisted use.