Simulate the weekly savings plan to fund Nexo's lab using a for loop, continue for skipped maintenance weeks, and break once the goal is reached.
JavaScriptBeginnerWith Nexo
bucles · break-continue · acumuladores
Statement
Your mission
Nexo needs to fund equipment for a new technology lab through a weekly savings plan. However, every certain number of weeks (frecuenciaOmitida), the budget goes toward maintenance and no deposit is made; furthermore, as soon as the savings goal is reached, the plan stops immediately without processing any additional weeks.
Implement the solve(input) function, which receives a JSON object with five integer properties:
ahorroInicial: integer between 0 and 100000 (in cents).
aporteSemanal: integer between 0 and 10000 (in cents).
metaAhorro: integer between 0 and 200000 (in cents).
maximoSemanas: integer between 0 and 52.
frecuenciaOmitida: integer between 1 and 12.
Simulation rules:
Initial check: if ahorroInicial >= metaAhorro before starting the loop, the plan finishes immediately with 0 weeks processed.
Weekly iteration: otherwise, iterate through weeks from 1 to maximoSemanas (both inclusive) with a for loop:
Every visited week counts as elapsed (including skipped weeks).
If semana % frecuenciaOmitida === 0, count the week as skipped and use continue without adding a contribution.
On all other weeks, add aporteSemanal to the accumulated savings, count the week as a contributed week (even when aporteSemanal is 0), and check whether accumulated savings are >= metaAhorro; if so, use break immediately so no subsequent weeks are processed.
If maximoSemanas runs out without reaching the goal, keep the accumulated savings up to that point.
Return a JSON object with those exact six properties:
ahorroFinal: integer with the accumulated savings in cents.
metaCumplida: boolean ahorroFinal >= metaAhorro.
semanasTranscurridas: total number of processed weeks (0 if none were processed).
semanasConAporte: number of non-skipped processed weeks.
semanasOmitidas: number of skipped processed weeks.