Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Calculating dice to use based on equation

1684712642

Edited 1684712701
Hello, I have an issue where the math is all wonky... I have a Dice Progression Chart which you roll from based on a simple equation: TL / Cost If the Tech Level is 32 And if you gain a +1 for every 4 levels (Cost... in levels) (This is an input field, the divisor changes). The total bonus should be +8 (32/4 = 8) I have the dice progression chart defined as such: /* DICE PROGRESSION CHART */             const dice_values = ['1d4', '1d6', '1d8', '1d10', '1d12', '1d20', '1D20+1D4', '1D20+1D6', '1D20+1D8', '1D20+1D10', '1D20+1D12', '1D20+1D20', '2D20+1D4', '2D20+1D6', '2D20+1D8', '2D20+1D10', '2D20+1D12', '2D20+2D20', '3D20+1D4', '3D20+1D6', '3D20+1D8', '3D20+1D10', '3D20+1D12', '3D20+3D20', '4D20+1D4', '4D20+1D6', '4D20+1D8', '4D20+1D10', '4D20+1D12', '4D20+4D20', '5D20+1D4', '5D20+1D6', '5D20+1D8', '5D20+1D10', '5D20+1D12', '5D20+1D20', '6D20+1D4', '6D20+1D6', '6D20+1D8', '6D20+1D10', '6D20+1D12', '6D20+1D20', '7D20+1D4', '7D20+1D6', '7D20+1D8', '7D20+1D10', '7D20+1D12', '7D20+1D20', '8D20+1D4', '8D20+1D6', '8D20+1D8', '8D20+1D10', '8D20+1D12', '8D20+1D20', '9D20+1D4', '9D20+1D6', '9D20+1D8', '9D20+1D10', '9D20+1D12', '9D20+1D20', '10D20+1D4', '10D20+1D6', '10D20+1D8', '10D20+1D10', '10D20+1D12', '10D20+1D20', '11D20+1D4', '11D20+1D6', '11D20+1D8', '11D20+1D10', '11D20+1D12', '11D20+1D20', '12D20+1D4', '12D20+1D6', '12D20+1D8', '12D20+1D10', '12D20+1D12', '12D20+1D20', '13D20+1D4', '13D20+1D6', '13D20+1D8', '13D20+1D10', '13D20+1D12', '13D20+1D20', '14D20+1D4', '14D20+1D6', '14D20+1D8', '14D20+1D10', '14D20+1D12', '14D20+1D20', '15D20+1D4', '15D20+1D6', '15D20+1D8', '15D20+1D10', '15D20+1D12', '15D20+1D20', '16D20+1D4', '16D20+1D6', '16D20+1D8', '16D20+1D10', '16D20+1D12', '16D20+1D20', '17D20+1D4', '17D20+1D6', '17D20+1D8', '17D20+1D10', '17D20+1D12', '17D20+1D20', '18D20+1D4', '18D20+1D6', '18D20+1D8', '18D20+1D10', '18D20+1D12', '18D20+1D20', '19D20+1D4', '19D20+1D6', '19D20+1D8', '19D20+1D10', '19D20+1D12', '19D20+1D20', '20D20+1D4', '20D20+1D6', '20D20+1D8', '20D20+1D10', '20D20+1D12', '20D20+1D20', '20D20+1D100', '5D100', '6D100', '7D100', '8D100', '9D100', '10D100+100',]; And here is the JS that does the calc to determine which dice to use. /* Repeating Inventory Weapon Dice Auto-Populator */                         on("change:repeating_weapons:weapontechlevel change:repeating_weapons:weapondamagecost", function() {                    getAttrs(["repeating_weapons_weapontechlevel","repeating_weapons_weapondamagecost"], function(values) {                             console.log(`values ${JSON.stringify(values)}`);                             let wd = parseInt(values.repeating_weapons_weapondamagecost) ;                             let tl = parseInt(values.repeating_weapons_weapontechlevel) / wd;                             console.log("tl: "+tl)                             let dice = dice_values[tl-1];                             console.log("dice: "+dice)                             setAttrs({repeating_weapons_weapondamage:dice});                         });             });     The answers it gives are not linear at all, and the sums jump around. Sometimes they are blank. I've tried it without the "/ wd". I don't understand the console.logs, either. Not sure why that is needed. Someone gave me this code.
1684757541

Edited 1684764756
GiGs
Pro
Sheet Author
API Scripter
Your worker looks properly constructed though I'd add || default like so: let wd = parseInt(values.repeating_weapons_weapondamagecost) || 1; let tl = (parseInt(values.repeating_weapons_weapontechlevel) || 0) / wd; Thats to make sure you are always getting a numerical value in those variables. I'd also constrain your dice value to min and max values to ensure the dice value doesn't break. let dice = dice_values[Math.min(dice.values.length-1 , Math.max(0, tl-1))]; This should solve the blank entries problem. The conole.logs are nt needed. You can delete them once you know the code is working. That's a simple form of debugging - the writer can see what the values are being calculated to be. Now, I don't know what you mean by the values jumping around. Is it calculating values wrongly? If so, which ones?
Hey GiGs. Yeah, instead of TL / #, it's TL + #.... or something. I didn't fully figure it out, it was random. Sometimes blank or just the wrong dice values. I'll try your edits! Thanks!