Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!