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

Attribute calculation with enforced range of values

Hi all, Have been putting together a sheet worker using a simple calculation to work out a total attribute value based on an inputted value and a modifier based on race. This works fine, but I need the final value to be forced to be between 3 and 18. Here is an example of my current code and what I would like it to do is if the value of "int" + the value of "calc_raceINT" results in a value higher than 18, the value of "totalINT" is set to 18. Race modifiers have both positive and negative and it will be rare to happen, but just wanted to cover this situation. // Update INT value based on race  on("sheet:opened change:race change:int change:calc_raceINT", function () {     getAttrs(["int","calc_raceINT"], function(values) {     var get_INT = parseInt(values["int"])||0;     var get_raceINT = parseInt(values["calc_raceINT"])||0;     var totalINT = get_INT + get_raceINT;     setAttrs({     finalINT: totalINT     }); });  }); Any thoughts? Thanks in advance.
1647718308
vÍnce
Pro
Sheet Author
I'm not the best person to give js advice... try changing var totalINT = get_INT + get_raceINT; to var totalINT = Math.max((get_INT + get_raceINT),18); <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max</a>
1647719255

Edited 1647720775
GiGs
Pro
Sheet Author
API Scripter
You actually want to use Max.min there (it's counterintuitive, but you are using the minimum of 2 values, so whichever is 18 or lower). To account for the lower value you do use Math.max. var totalINT = Math.max(Math.min(get_INT + get_raceINT, 18), 3);
1647719496

Edited 1647719522
GiGs
Pro
Sheet Author
API Scripter
You can also do it with if else - using math.max/min is more concise, but if/else can be easier to understand. Either way does the job. var totalINT = get_INT + get_raceINT; if(totalInt &gt; 18) { totalInt = 18; } else if(totalInt &lt; 3) { totalInt = 3; }
1647720173
vÍnce
Pro
Sheet Author
Everything I don't know I taught myself.&nbsp; Everything I might know about javascript I learned from GiGs. ;-)
Fantastic! Thanks very much, I have been staring at the wiki and other posts and it just wasn't making sense before. This works perfectly, thanks again! GiGs said: You actually want to use Max.min there (it's counterintuitive, but you are using the minimum of 2 values, so whichever is 18 or lower). To account for the lower value you do use Math.max. var totalINT = Math.max(Math.min(get_INT + get_raceINT, 18), 3);