0

So basically my issue is that I'm looking to get some data from an object. The object is picked from and html tag and the selected option is put into a function. The function handles getting the data but throws an error when trying to get the length of an array found inside the object. getRandInRange is a function I created that works perfectly so that is not the issue.

When I run the code in the browser this is the error I get

Uncaught TypeError: Cannot read properties of undefined (reading 'length')
    at buildHorde (app.js:46:60)
    at setValue (app.js:33:5)
    at HTMLInputElement.onclick (index.html:27:101)
function buildHorde(obj,num){
    // for every num add a random coin roll
    for (i = 0; i < num; i ++){
        coinArr.push(obj.coins[getRandInRange(0, obj.coins.length-1)]);
    }
    // following lines add the new arrays into the html document.
    document.getElementById("coins").innerHTML = coinArr;
    return;
}
<p>Select Party Level: <select name="Party Level" id="getObj">
    <option value=level1>level1</option>
    <option value=level2>level2</option>
</select></p>

And this is the code to set the arguments in the function

function setValue(){
    var select = document.getElementById('getObj');
    obj = select.options[select.selectedIndex].value
    num = document.getElementById('getNum').value;
    buildHorde(obj,num);
}

level1 object

let level1 = {
    coins: ["No coin", getRandInRange(1,6) * 1000 + " Copper Pieces ", getRandInRange(1,8) * 100 + " Silver Pieces ", getRandInRange(1,4) * 10 + " Platinum Pieces "],
    goods: ["nothing ", "Extravagant painting ", "ornate statue "],
    items: ["Boots of speed ", mundane.weapon[getRandInRange(0, mundane.weapon.length-1)], "Potion of fire breathing "],
}
  • Welcome to StackOverflow! Can you be more specific on what part of the code is not working and what the error message is? – nfv May 30 '22 at 01:48
  • @nfv Yeah I've gone ahead and done that now. – Bailey Long May 30 '22 at 01:50
  • It seems to me like `obj.coins` is not defined, or at least is not an array. What is returned from `select.options[select.selectedIndex].value`? – nfv May 30 '22 at 01:53
  • @nfv The return is level1 which is what I want it to be and yes coins is defined inside of the object level1 and is an array. – Bailey Long May 30 '22 at 01:56
  • 1
    `obj` is a string (as are pretty much all values you get from DOM objects' `value` properties). If you want `obj` to refer to a `level1` variable, there are other ways of doing that. – Heretic Monkey May 30 '22 at 02:01
  • no @HereticMonkey that doesn't seem to be it... thanks anyway – Bailey Long May 30 '22 at 02:12
  • Post what `obj` is supposed to look like because as the code is now `obj = "Level1"` (or `"Level2"`) is what nfv and HM is trying to tell you. You are expecting something like this: `obj = {coins: [1,5,9,1,0,]}` but the `select#getObj` is giving literally a string that is `"Level1"`. – zer00ne May 30 '22 at 02:31
  • @zer00ne I have added the level1 object into the post – Bailey Long May 30 '22 at 02:33
  • @BaileyLong you don't have gold pieces? – zer00ne May 30 '22 at 02:39
  • @zer00ne no I forgot to add them in, I threw the array together very quickly – Bailey Long May 30 '22 at 02:40
  • @zer00ne so to fix this I would need to get the absolute value back from the form rather than a string correct? – Bailey Long May 30 '22 at 02:44
  • This question's problem boils down to "How do I access a variable by its name dynamically?", which is not the same as the linked question. Though this question's problem did only arise from a misunderstanding between strings and variables of similar name, as _HereticMonkey_ and _zer00ne_ have pointed out. – Oskar Grosser May 30 '22 at 03:26

0 Answers0