Transform numeric and boolean text strings using Number() and strict comparison while preserving string identifiers with leading zeros.
JavaScriptBeginnerWith Nexo
tipos-de-datos · conversion-de-tipos · operadores
Statement
Your mission
When a system receives data from forms or text files, values such as prices, quantities, and boolean flags often arrive as character strings (string). Knowing when to convert text to a number, how to interpret a text boolean without falling into traps like Boolean("false"), and when to preserve an identifier as text is essential for processing data properly.
Implement the solve(input) function, which receives a JSON object with four properties:
precioTexto: string of decimal digits representing an integer between 0 and 100000 (may contain leading zeros).
cantidadTexto: string of decimal digits representing an integer between 0 and 1000 (may contain leading zeros).
descuentoTexto: string that is exactly "true" or "false" in lowercase.
codigo: non-empty string of 1 to 20 characters identifying the product (may contain leading zeros, such as "0042").
Return a JSON object with those exact six properties:
precio: the numeric value resulting from Number(input.precioTexto).
cantidad: the numeric value resulting from Number(input.cantidadTexto).
descuentoActivo: boolean true only when descuentoTexto === "true" (and false otherwise).
total: the product precio * cantidad (the discount flag is informational only and never modifies total).
codigo: the original identifier received in input.codigo, without numeric conversions.
resumen: a string with the exact format "Producto CODIGO: CANTIDAD unidades por TOTAL pesos.", replacing CODIGO, CANTIDAD (the converted number), and TOTAL with their corresponding values.
Rules and constraints
Do not use if, else, switch, loops, arrays, classes, or helper functions.
Do not use Boolean(descuentoTexto) to compute descuentoActivo; use a strict comparison with "true".
Do not convert codigo to a number, as that would strip leading zeros from the identifier.