Example 1
Input
{
"numeroA": 125,
"numeroB": 75,
"operacion": "SUMAR"
}Expected result
{
"exitosa": true,
"mensaje": "Operación completada",
"operacion": "SUMAR",
"resultado": 200
}Nexo is preparing the challenges…
Nexo · Connect the ideas
Build Nexo's modular lab calculator by implementing sumar, restar, multiplicar, and dividir helper functions and coordinating their invocation from solve(input).
funciones · parametros-retorno · modularidad
At Nexo's technology lab, you are building a modular calculator. Instead of placing all calculations inside a single function, each arithmetic operation is encapsulated in its own reusable function with parameters and a return value, while solve(input) coordinates which function to invoke and protects the system against division by zero.
Implement the following four mandatory helper functions:
sumar(a, b): returns a + b.restar(a, b): returns a - b.multiplicar(a, b): returns a * b.dividir(a, b): returns Math.round((a / b) * 100) / 100.Additionally, implement the coordinator function solve(input), which receives a JSON object with three properties:
numeroA: integer between -10000 and 10000.numeroB: integer between -10000 and 10000.operacion: exactly "SUMAR", "RESTAR", "MULTIPLICAR", or "DIVIDIR".Coordination rules in solve(input):
operacion === "DIVIDIR" and numeroB === 0, do not invoke dividir and return a controlled error with resultado: null, exitosa: false, and mensaje: "No se puede dividir por cero".if / else if / else or switch, invoke the corresponding helper function passing numeroA and numeroB as arguments, and use its return value as resultado, with exitosa: true and mensaje: "Operación completada"."DIVIDIR" must preserve their exact integer results without rounding.Return a JSON object with those exact four properties:
operacion: the original string received.resultado: the number returned by the helper function, or null when dividing by zero.exitosa: boolean (false only when dividing by zero).mensaje: "Operación completada" if the operation is valid, or "No se puede dividir por cero" if it fails.sumar, restar, multiplicar, and dividir) with return (not console.log).Input
{
"numeroA": 125,
"numeroB": 75,
"operacion": "SUMAR"
}Expected result
{
"exitosa": true,
"mensaje": "Operación completada",
"operacion": "SUMAR",
"resultado": 200
}Input
{
"numeroA": 10,
"numeroB": 3,
"operacion": "DIVIDIR"
}Expected result
{
"exitosa": true,
"mensaje": "Operación completada",
"operacion": "DIVIDIR",
"resultado": 3.33
}Input
{
"numeroA": 45,
"numeroB": 0,
"operacion": "DIVIDIR"
}Expected result
{
"exitosa": false,
"mensaje": "No se puede dividir por cero",
"operacion": "DIVIDIR",
"resultado": null
}Implement solve(input) and return the result. Synchronous JavaScript, without packages, network or account access.
418/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.