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

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

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

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