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");
}
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.