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

Help with calculations via scripts

1590949784

Edited 1590949806
I got a script that works out the mod for a score using if statment but its getting really long and has a cap to it const stats = ["melee","ranged","str","con","per","move","ste","fin","cont","cha","ter","int","wit","tin"]; stats.forEach(stat => { on(`change:${stat}`, () => { getAttrs([stat], values => { const stat_base = int(values[stat]); console.log(stat_base); let stat_bonus = 0; if (stat_base <=1) stat_bonus = "-5"; else if (stat_base <=3) stat_bonus = "-4"; else if (stat_base <=5) stat_bonus = "-3"; else if (stat_base <=7) stat_bonus = "-2"; else if (stat_base <=9) stat_bonus = "-1"; else if (stat_base <=11) stat_bonus = "0"; else if (stat_base <=13) stat_bonus = "1"; else if (stat_base <=15) stat_bonus = "2"; else if (stat_base <=17) stat_bonus = "3"; else if (stat_base <=19) stat_bonus = "4"; else if (stat_base <=21) stat_bonus = "5"; else if (stat_base <=23) stat_bonus = "6"; else if (stat_base <=25) stat_bonus = "7"; else if (stat_base <=27) stat_bonus = "8"; else if (stat_base <=29) stat_bonus = "9"; else if (stat_base <=31) stat_bonus = "10"; else if (stat_base <=33) stat_bonus = "11"; else if (stat_base <=35) stat_bonus = "12"; else if (stat_base <=37) stat_bonus = "13"; else if (stat_base <=39) stat_bonus = "14"; else if (stat_base <=41) stat_bonus = "15"; else if (stat_base <=43) stat_bonus = "16"; else if (stat_base <=45) stat_bonus = "17"; else if (stat_base <=47) stat_bonus = "18"; else if (stat_base <=49) stat_bonus = "19"; else if (stat_base <=51) stat_bonus = "20"; else if (stat_base <=53) stat_bonus = "21"; else if (stat_base <=55) stat_bonus = "22"; else if (stat_base <=57) stat_bonus = "23"; else if (stat_base <=59) stat_bonus = "24"; else if (stat_base <=61) stat_bonus = "25"; else if (stat_base <=63) stat_bonus = "26"; else if (stat_base <=65) stat_bonus = "27"; else if (stat_base <=67) stat_bonus = "28"; else if (stat_base <=69) stat_bonus = "29"; else if (stat_base <=71) stat_bonus = "30"; else if (stat_base <=73) stat_bonus = "31"; else if (stat_base <=75) stat_bonus = "32"; else if (stat_base <=77) stat_bonus = "33"; else if (stat_base <=79) stat_bonus = "34"; else if (stat_base <=81) stat_bonus = "35"; else if (stat_base <=83) stat_bonus = "36"; else if (stat_base <=85) stat_bonus = "37"; else if (stat_base <=87) stat_bonus = "38"; else if (stat_base <=89) stat_bonus = "39"; else if (stat_base <=91) stat_bonus = "40"; else if (stat_base <=93) stat_bonus = "41"; else if (stat_base <=95) stat_bonus = "42"; else if (stat_base <=97) stat_bonus = "43"; else if (stat_base <=99) stat_bonus = "44"; else if (stat_base <=101) stat_bonus = "45"; else if (stat_base <=103) stat_bonus = "46"; else if (stat_base <=105) stat_bonus = "47"; else if (stat_base <=107) stat_bonus = "48"; else if (stat_base <=109) stat_bonus = "49"; else if (stat_base <=111) stat_bonus = "50"; else if (stat_base <=113) stat_bonus = "51"; else if (stat_base <=115) stat_bonus = "52"; else if (stat_base <=117) stat_bonus = "53"; else stat_bonus = "error" setAttrs({ [`${stat}_bonus`]: stat_bonus }); }); }); }); so i was wondering if i could do something like it  after the base stat is 12 or bigger it would take away 10 from the the base stat then divide it by 2 then round down and set that as the stat bonus
1590952932
GiGs
Pro
Sheet Author
API Scripter
If you dont care about an upper limit, you can replace the entire calculation with:         const stat_bonus = Math.max(-5, Math.floor(stat_base /2) -5); If its important that you maintain the 117 maximum score, use         const stat_bonus = stat_base <= 117 ? Math.max(-5, Math.floor(stat_base /2) -5): 'error';
1590953351

Edited 1590953403
GiGs
Pro
Sheet Author
API Scripter
Math.floor( calculation) does the rounding down: Math.floor(stat_base /2) Math.max is given a list of numbers or calculations separated by commas, and returns the higheest of them, so this: Math.max(-5, a calculation); can never return a number less than -5.  And finally we have my favourite javascript construction, the ternary operator , which is an if statement that returns a single value: variable = (calculation) ? (value if calculation true) : (value if calculation false); The ? and : separate the expression into three sections, which must always be present. The calculation at the start must return true or false (or must be truthy or falsy in javascript terms), and that sets which of the two following values are used. So here's that last expression with brackets added to make the sections clearer. This: const stat_bonus = ( stat_base <= 117 ) ? ( Math.max(-5, Math.floor(stat_base /2) -5) ): ( 'error' ) ; is the same as writing: let stat_bonus; if(stat_base <= 117) {     stat_bonus =   Math.max(-5, Math.floor(stat_base /2) -5); else {     stat_bonus = 'error' } but is a lot more concise.
Thank you soooo much <3
1590957296
GiGs
Pro
Sheet Author
API Scripter
you're welcome :)
1590958482
vÍnce
Pro
Sheet Author
I write 1000 lines of code and GiGs comes along and optimizes it in 10.  And his works.  ;-P Kidding aside, your posts and efforts are sincerely appreciated by all.  Thanks
The Aaron may be The Scriptomancer, but GiGs is like The Code-Tightener.
1590976396
GiGs
Pro
Sheet Author
API Scripter
Hehe, thanks guys. I appreciate it. That said, Aaron's code is tighter than mine, but i dont think he monitors the character sheet subforum, giving us mere mortals a chance to shine.