javascriptarraysobjectkeyprobability

Picking a random object inside an array based on probability stored in keys


I'm just learning javascript and one of my personal projects for learning is a rpg game. I've been trying to get this working for several days, using functions I've seen on stackoverflow and trying to edit them for my particular case, but nothing seems to work. The game is text based and now im working on a "loot system", so what I'm looking for is something like this: When a "search" button is clicked, a function is activated that reviews the objects in a given array, and selects one object based on the probabilities. I assigned each object a key called por: and that is the probability I would like for said object to come out.

btw sorry for my english.

const itemOne = {name: "Numero uno", por: 0.20, info: "some info about the item"};
const itemTwo = {name: "Numero dos", por: 0.10, info: "some info about the item"};
const itemThree = {name: "Numero tres", por: 0.15, info: "some info about the item"};
const itemFour = {name: "Numero cuatro", por: 0.15, info: "some info about the item"};
const itemFive = {name: "Numero cinco", por: 0.25, info: "some info about the item"};
const itemSix = {name: "Numero seis", por: 0.10, info: "some info about the item"};
const itemSeven = {name: "Numero siete", por: 0.05, info: "some info about the item"};


let lootAvailable = [itemOne, itemTwo, ItemThree, ItemFour, ItemFive, ItemSix, ItemSeven];

below is the last function I copied from stackoverflow. I have tried several and I'm unable to make them work. To be completely honest with you, I do not understand fully how the function works, and I haven't been trough objects or loops in the curriculum im working on yet. But this game is a lot of fun to make for me so... maybe im chewing more than I can bite for now?

function search(lootAvailable) {
    var winner = Math.random();
    var threshold = 0;
    for (let i = 0; i < lootAvailable.length; i++) {
        threshold += parseFloat(lootAvailable[i].por);
        if (threshold > winner) {
            return lootAvailable[i];   
        }
    }
}

when I try this on the game, in the console show the following error: juegorpg.js:54 Uncaught TypeError: Cannot read properties of undefined (reading 'length')

So that. I would like to know what would be the most efficient way to get an object from a list of arrays, based on the probability of the keys.

again, sorry for my english and lack of experience.


Solution

  • You should pass lootAvailable either as an argument:

    const itemOne = {name: "Numero uno", por: 0.20, info: "some info about the item"};
    const itemTwo = {name: "Numero dos", por: 0.10, info: "some info about the item"};
    const itemThree = {name: "Numero tres", por: 0.15, info: "some info about the item"};
    const itemFour = {name: "Numero cuatro", por: 0.15, info: "some info about the item"};
    const itemFive = {name: "Numero cinco", por: 0.25, info: "some info about the item"};
    const itemSix = {name: "Numero seis", por: 0.10, info: "some info about the item"};
    const itemSeven = {name: "Numero siete", por: 0.05, info: "some info about the item"};
    
    
    let lootAvailable = [itemOne, itemTwo, itemThree, itemFour, itemFive, itemSix, itemSeven];
    
    function search(lootAvailable) {
        var winner = Math.random();
        var threshold = 0;
        for (let i = 0; i < lootAvailable.length; i++) {
            threshold += parseFloat(lootAvailable[i].por);
            if (threshold > winner) {
                return lootAvailable[i];   
            }
        }
    }
    
    console.log(search(lootAvailable))

    Or just remove it:

    const itemOne = {name: "Numero uno", por: 0.20, info: "some info about the item"};
    const itemTwo = {name: "Numero dos", por: 0.10, info: "some info about the item"};
    const itemThree = {name: "Numero tres", por: 0.15, info: "some info about the item"};
    const itemFour = {name: "Numero cuatro", por: 0.15, info: "some info about the item"};
    const itemFive = {name: "Numero cinco", por: 0.25, info: "some info about the item"};
    const itemSix = {name: "Numero seis", por: 0.10, info: "some info about the item"};
    const itemSeven = {name: "Numero siete", por: 0.05, info: "some info about the item"};
    
    
    let lootAvailable = [itemOne, itemTwo, itemThree, itemFour, itemFive, itemSix, itemSeven];
    
    function search() {
        var winner = Math.random();
        var threshold = 0;
        for (let i = 0; i < lootAvailable.length; i++) {
            threshold += parseFloat(lootAvailable[i].por);
            if (threshold > winner) {
                return lootAvailable[i];   
            }
        }
    }
    
    console.log(search(lootAvailable))