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

187
Views
How to add weight of item for randomizing

I am learning to code Javascript and I am currently trying this out! I have an object of 'player' and with chestRewards. In the chest, there are some items to be randomly given. How can I add weight to different items in the array so that item 'gold' is not so frequently given out(rare item). I am also confused with the output, it is printing undefined but I don't understand where that is coming from.

const lootBoxA = ['gold', 'silver', 'shirt', 'shorts'];

const player = {

  chestReward: lootBoxA,

    currentReward(){

      while (this.currentReward){
        this.currentReward = this.chestReward[Math.floor(Math.random() * 4)];
        console.log(this.currentReward);

      if (this.currentReward === 'gold'){
        console.log('Bonus!');
      }break;
    }
  }
}
console.log(player.currentReward()); 
/*Outputs 
gold
Bonus!
undefined*/
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You will need more information than just the name of your items. So, instead of representing your items with string, you can use objects :

const lootBoxA = [
  { name: 'gold', probability: 0.1 },
  { name: 'silver', probability: 0.3 },
  { name: 'shirt', probability: 0.7 },
  { name: 'shorts', probability: 0.5 }
];

That way, you give each item a probability to get picked.

Then, I see no reason why the current reward of your player should be a function (currentReward()). It can just be a property, so you would have something like that :

const player = {
  name: 'Henry',        // for example
  currentReward: null   // the player starts without reward
}

You can have a function to pick a reward for your player. It will take a "lootBox" and pick an item with the right probability :

function pickItemFromLootBox(lootBox) {
  const randomIndex  = Math.floor(Math.random() * lootBox.length);
  const randomNumber = Math.random(); // between 0 and 1
  
  const selectedLootBoxItem = lootBox[randomIndex];

  if (selectedLootBoxItem.probability >= randomNumber) {
    return selectedLootBoxItem;
  } else {
    return null; // no item selected
    // or if you want to always select an item, replace the above line with this : 
    // return pickItemFromLootBox(lootBox);
  }
}

Now that you have a player, a lootBox and a function to pick an item from the lootBox, you can do what you want :

player.currentReward = pickItemFromLootBox(lootBoxA);

if (player.currentReward !== null && player.currentReward.name === 'gold') {
  console.log('Bonus !')
}
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!