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

155
Views
Why does this method not give the right multiples of 0.25 in javascript

I am trying to get the multiples of 0.25 but i am getting the wrong output. What am i doing wrong?

let arr1 = []
for (let i = 0.25; i <= 4.25; i++) {
  //console.log(change)
  if (i > 3) {
    continue;
  }
  i % 0.25 == 0 ? arr1.push(i) : 'cancl'
  console.log(arr1) // i get [0.25, 1.25, 2.25] instead of [0.25, 0.5, 0.75, ...]
}

How can i get the write answer/multiples?

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

0

Your code doesn't work because i++ increments i by 1. Increment i by 0.25 instead:

const arr1 = []
for (let i = .250; i <= 4.25; i += 0.25) {
  arr1.push(i)
}

console.log(arr1)

about 4 years ago · Juan Pablo Isaza Report

0

The simple flaw in the code is that it is getting incremented by 1.

The condition should be i = i+ .25

Checkout the code below

let arr1 = []
for (let i = 0.25; i <= 4.25; i=i+.25) {
  //console.log(change)
  if (i > 3) {
    continue;
  }
  i % 0.25 == 0 ? arr1.push(i) : 'cancl'
   // i get [0.25, 1.25, 2.25] instead of [0.25, 0.5, 0.75, ...]
}

console.log(arr1)

about 4 years ago · Juan Pablo Isaza Report

0

Because using i++, you will get i + 1 after iterating

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!