Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda