Code:var lootedItem; var loot = { "silver": 3, "bronze": 3, "copper": 3, "platinum": 1, "iron": 5, "stone": 5 }; var playerLoot = { "silver": 0, "bronze": 0, "copper": 0, "platinum": 0, "iron": 0, "stone": 0 };The array "loot[]" is supposed to be a loot table with a corresponding value. When the function mining() is called, it should randomly return 1 of the values from the array, with the corresponding value.Code:function mining() { //gold++; for(var v=0; v <loot.length; v++){ lootedItem = loot[Math.floor(Math.random() * loot.length)]; playerLoot[v]+= lootedItem[v]; } console.log(lootedItem); //RETURNS 0 every time console.log(playerLoot); //returns the players array of 0's }
So the first time it's called, maybe it randomly gives iron, it should give 5 iron to the player's iron variable.
So if it randomly gives you the loot[5] entry as loot, it should increment the corresponding playerLoot[5].
Anyone point me in the right direction? Been glued to the array documentation but not able to get it working....
For right now if I could just get it to return a random entry I can figure out how to assign it to the corresponding entry in the other array...Any ideas how to get it to work right?
returns 0 every time, I tried various combinations addingCode:lootedItem = loot[Math.floor(Math.random() * loot.length)];andCode:lootedItem[v]= loot[Math.floor(Math.random() * loot.length)][0];thinking that would return the first value from the random entry but it doesnt work either.Code:lootedItem[v][0]= loot[Math.floor(Math.random() * loot.length)][0]; lootedItem[0][0]= loot[Math.floor(Math.random() * loot.length)][0]; //etc various combinations...



Reply With Quote




Bookmarks