Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I check if multiple random chances are 1 efficiently?

So, I have 4 chances (chance1, chance2, etc.). I want to check if one chance is equal to one, but none of the other ones are one. I know I could just check every single one, but I’m eventually going to add more and more chances, and the list will be really long and annoying to deal with.

var chance1 = Math.floor(Math.random() * 10) + 1
var chance2 = Math.floor(Math.random() * 100) + 1
var chance3 = Math.floor(Math.random() * 1000) + 1
var chance4 = Math.floor(Math.random() * 10000) + 1

if (chance1 === 1) {
  document.body.innerText = "Red - 1 in 10"
  document.body.style.backgroundColor = "red"
}

if (chance2 === 1) {
  document.body.innerText = "Orange - 1 in 100"
  document.body.style.backgroundColor = "orange"
}

if (chance3 === 1) {
  document.body.innerText = "Yellow - 1 in 1,000"
  document.body.style.backgroundColor = "yellow"
}

if (chance4 === 1) {
  document.body.innerText = "Green - 1 in 10,000"
  document.body.style.backgroundColor = "green"
}

Again, I tried checking every single value, it works, but I think it would be best to have a more efficient way to this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

So you want to update the page to show the least likely event that happened?

Store your chances in an array, along with the color that corresponds with that chance. There are several ways to do this, one would be to store them as an object that looks like this

{
  chance: 1000,
  color: 'green',
}

Store the chances in reverse order, meaning test the least likely scenario first, as opposed to checking the most likely and then overwriting afterward.

So the full code might look something like this

const chances = [
  { chance: 10000, color: 'green' },
  { chance: 1000, color: 'yellow' },
  { chance: 100, color: 'orange' },
  { chance: 10, color: 'red' },
];

function determineChance() {
  for (let {chance, color} of chances) {
    const roll = Math.floor(Math.random() * chance) + 1;
    if (roll === 1) {
       const capColor = color[0].toUpperCase() + color.slice(1);
       document.body.innerText = `${capColor} - 1 in ${chance}`;
       document.body.style.backgroundColor = color;
       
       return;
    }
  }
  
  // If here, no hits
  document.body.innerText = 'No hits';
}

determineChance();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading