Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda