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

99
Views
Loop is not incrementing

I am new to javascript.

I have to create a function that returns the next multiples of 5 based on a given number.But my loop repeats the information, it doesn´t increment the result.What am I doing wrong?

function proS(num, mult) {
  let arr = []

  for (var i = 1; i <= mult; i++) {
    arr.push(Math.ceil(num / 5) * 5)
  }
  return arr
}

proS(6, 4)

returns [10, 10, 10, 10]

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

you should use your formula to produce the initial value of num, then add 5 to it each time through the loop.

function proS(num, mult) {
  let arr = []
  num = Math.ceil(num / 5) * 5;
  for (var i = 1; i <= mult; i++, num += 5) {
    arr.push(num)
  }
  return arr
}

console.log(proS(6, 4))

about 4 years ago · Juan Pablo Isaza Report

0

I won't write the answer, but I'll point you in the right direction. If you pay close attention arr.push(Math.ceil(num / 5) * 5) this bit of code is invariant. This means that you should probably add the variable i somewhere in there so that it actually returns a different output every iteration of the loop.

about 4 years ago · Juan Pablo Isaza Report

0

thanks guys, I ended up doing this and it worked:

function proS (num, mult){
let arr = []
let res = (Math.ceil(num / 5) * 5)

for (var i = 1; i <= mult; i ++){
arr.push(res)
res += 5
}
return arr
}
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!