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

75
Visualizações
Random generated array push

I'm attempting to test feature to only push to this array 10% of the time, I've come up with a solution that works but is there a more dynamic way of handling this?

This is what I came up with:

const random = []

const randomNumber = Math.round(Math.random() * 9); 
console.log(randomNumber)

if (randomNumber === 5) {
random.push({
title: "testing"
})

console.log(random, 'testing push')
} else {
console.log('not this time')
}

I'm selecting 10 possibly numbers, if the number equals 5 then push to the array, if not don't. This would make it a 1/10 chance if it pushing it to the array.

Is it possible to create a more dynamic solution of saying 10% of the time push the object to the array?

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

0

I would say you can simplify your formula:

const percentage=10;

if (Math.random() <== percentage/100){
    //Should be executed 10% of the time 

}
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