Example 1
Input
{
"fin": 0,
"paso": 2,
"inicio": 6,
"incluirFin": true
}Expected result
{
"secuencia": [
6,
4,
2,
0
],
"iteraciones": 4,
"ultimoValor": 0,
"alcanzoLimite": true
}Nexo is preparing the challenges…
Nexo · Connect the ideas
Build a controlled energy countdown using while or for loops, respecting the decrement step and whether the target boundary is inclusive or exclusive.
bucles · while-for · iteraciones
To activate a Nexo device, you need to calibrate a controlled energy countdown. The pulse starts at an initial level and steps down in fixed increments (paso) toward a threshold (fin), stopping immediately before dropping below the allowed limit.
Implement the solve(input) function, which receives a JSON object with four properties:
inicio: integer between 0 and 100.fin: integer between 0 and inicio.paso: integer between 1 and 20.incluirFin: boolean.Descending sequence rules:
inicio and subtract paso in each iteration.incluirFin is true, include only values greater than or equal to fin (>= fin).incluirFin is false, include only values strictly greater than fin (> fin).paso causes the counter to jump from a value above fin to a value below fin, the loop must terminate without including that value or artificially adjusting the sequence.inicio === fin, the sequence will contain a single element ([inicio]) when incluirFin is true, and will be empty ([]) when incluirFin is false.Return a JSON object with those exact four properties:
secuencia: array of integers with the generated values in descending order.iteraciones: integer with the number of values included in secuencia.ultimoValor: the last element included in secuencia, or null if secuencia is empty.alcanzoLimite: boolean true exclusively when fin appears in secuencia.while or for loop. Using push(), length, and index access to the last element is allowed.Array.from(), fill(), map(), recursion, classes, or helper functions.secuencia is empty, ultimoValor must be null (never undefined).Input
{
"fin": 0,
"paso": 2,
"inicio": 6,
"incluirFin": true
}Expected result
{
"secuencia": [
6,
4,
2,
0
],
"iteraciones": 4,
"ultimoValor": 0,
"alcanzoLimite": true
}Input
{
"fin": 2,
"paso": 3,
"inicio": 10,
"incluirFin": true
}Expected result
{
"secuencia": [
10,
7,
4
],
"iteraciones": 3,
"ultimoValor": 4,
"alcanzoLimite": false
}Input
{
"fin": 5,
"paso": 1,
"inicio": 5,
"incluirFin": false
}Expected result
{
"secuencia": [],
"iteraciones": 0,
"ultimoValor": null,
"alcanzoLimite": false
}Implement solve(input) and return the result. Synchronous JavaScript, without packages, network or account access.
132/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.