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

158
Views
Is there any better way of coding this as it feels very impractical and takes a lot of space

Is there any better way of coding this as it feels very impractical and takes a lot of space. It is basically a random chance of getting a rarity, for example Getting a Uncommon sword.

if (Math.random() * 100 < 100 / (19.37 / NewData.PriceDivider)) {
        Rarity = Rarities.uncommon;
        Embed.addField("Rarity", Rarity, true);
        Embed.setColor("GREEN");
      } else if (Math.random() * 100 < 100 / (5.18 / NewData.PriceDivider)) {
        Rarity = Rarities.common;
        Embed.addField("Rarity", Rarity, true);
        Embed.setColor("GREY");
      } else if (Math.random() * 100 < 100) {
        Rarity = Rarities.basic;
        Embed.addField("Rarity", Rarity, true);
        Embed.setColor("GREY");
      }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Maybe you could avoid rewriting the assignment statements by using ternary operator :

let rdm = Math.random() * 100
const firstCase = 19.37 / NewData.PriceDivider
const secondCase = 19.37 / 100 / (5.18 / NewData.PriceDivider)

// Handle rdm === 100 here  if (rdm === 100) // do something

Rarity = rdm < firstCase ? Rarities.uncommon : rdm < secondCase ? Rarities.common : Rarities.basic
Embed.addField("Rarity", Rarity, true);
Embed.setColor(rdm < firstCase ? "GREEN" : "GREY");

If you want to handle the case where Math.random() === 1, I think you should handle it before this code block.

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!