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

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

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 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