Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
How do I increase number S, N times, until it reaches number F?

I have a known startValue, a known finalValue and a fixed number of allowed iterations.

Starting with startValue I need to keep increasing the value each time round the loop until I reach finalValue.

I want the increase to be proportional (gradual).

I'm trying the below and the numbers seem to make sense except for the jump at the start and end of the array.

There seems to be a big jump from the 1st value to the 2nd and another big jump from the one before last to the last one.

The values in between seem to be increasing gradually though.

Is this the correct or best approach?

let startValue = 0.07
let finalValue = 4.76
let iterations = 10

let values = [startValue]

iterations--

while (iterations) {
  let [previousValue] = values.slice(-1)
  let nextValue = (finalValue - previousValue) / iterations
  values.push(nextValue)
  iterations--
}

values.push(finalValue)

document.getElementById('values').innerHTML = `[<br>&nbsp;&nbsp;${values.join(',<br/>&nbsp;&nbsp;')}<br>]`
<div id="values"></div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can generate an array based on the following logic:

1st item: startValue

2nd item: startValue + diff

3rd item: startValue + (diff * 2)

and so on...

const 
  startValue = 0.07,
  finalValue = 4.76,
  iterations = 10,
  diff = (finalValue - startValue) / (iterations - 1),
  values = Array.from({ length: iterations }, (_, i) => startValue + i * diff);

console.log(values);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda