Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!