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

209
Visualizações
Add set amount of items to an array JavaScript

I want to find how many 25 can fit into a number example 100 should return [25, 25, 25 ,25]
At the moment I have this code

let quarterAmount = parseInt(argument / 25)
sum.push( 25 * quarterAmount )


However when I return the sum variable it will give me [100] instead of the desired array of 25. I have tried many variations of my code and I always get the same results

sum.push( '25' * quarterAmount )
sum.push( [25 * quarterAmount] )
sum.push( [25] * quarterAmount )

I want to be able to add quarterAmount amount of [25] to my array, how can I acomplish this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Something like this?

const howManyTimes = (a, b) => new Array(b/a).fill(a)

console.log(howManyTimes(25, 100))

new Array(n) creates an array with n items, then fill sets all the items to the same value.

about 4 years ago · Juan Pablo Isaza Relatório

0

The naive solution would be to make a loop like this:

let quarterAmount = floor(argument / 25);
sum = [];
for (let i=0; i < quarterAmount; i++) {
    sum.push(25);
}

But there's a more convenient way of achieving what you want:

let quarterAmount = floor(argument / 25);
sum = Array(quarterAmount).fill(25);

Here is the related documentation (about Array.fill): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill

On a side note, I think you shouldn't use parseInt, but rather floor, ceil or round. parseInt will floor your value, but your intent is not to parse something, it is to round/floor/ceil something.

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