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 .
×
May your rolls be merry + bright! 🎄
Create a free account

ParseFloat default not working

I'm working to maintain the D&amp;D_OED sheet which was mostly set up by another person. The worker script includes a piece to sum up a series of encumbrance-value fields ( attr_enc_1 and so forth), that looks like this: &nbsp;&nbsp;&nbsp; // Gather encumbrance fields &nbsp;&nbsp;&nbsp; const items = []; &nbsp;&nbsp;&nbsp; for ( i = 1; i &lt;= 40; i++ ) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; items.push( 'enc_' + i ); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; items.reverse(); &nbsp;&nbsp;&nbsp; // Compute total encumbrance &nbsp;&nbsp;&nbsp; var total = 0; &nbsp;&nbsp;&nbsp; items.forEach( item =&gt; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; on( `change:${item} sheet:opened`, () =&gt; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getAttrs( items, values =&gt; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; items.forEach( enc =&gt; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; total = total + parseFloat( values[enc] )||0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setAttrs({total_enc: total}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }); &nbsp;&nbsp;&nbsp; }); Note that inside the forEach() loop there's the code parseFloat( values[enc] )||0 , which is meant to give a default value of zero if the field is blank or a non-number (per <a href="https://wiki.roll20.net/Sheetworker_examples_for_Non-programmers#Get_Attributes" rel="nofollow">https://wiki.roll20.net/Sheetworker_examples_for_Non-programmers#Get_Attributes</a>), and does so in other instances on this sheet. However, this default seems to be failing in the context of the forEach() loop. If any such field is blank or a non-number (e.g., "-" or "x", which might be used to indicate a null value), then the loop seems to stop processing and fails to add up any later values. Some examples: {1, 2, 3, 4, 5} -&gt; Total is 15 (correct) {1, 2, 3, 4, -} -&gt; Total is 10 (okay) {-, 2, 3, 4, 5} -&gt; Total is 0 (incorrect) {1, 2, -, 4, 5} -&gt; Total is 3 (incorrect) Note that blank or non-number fields later on don't cause a problem (e.g., most of the fields from some point are always blank). I assume that's why the original author was careful to include the items.reverse() function. But I'd prefer that blank or non-number fields near the start of the sequence not break the summing process. How can I fix this so the parseFloat default works inside the forEach() loop?
1676403743
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
This is a problem with order of operations. In plain english, what your line of code is saying is: Add the total to the parseFloat result, or use 0 What you want is: Add the total to either the result of parseFloat or 0 if parseFloat fails The code for that would be: total = total + (parseFloat( values[enc] ) || 0); Note I added some spaces around the bars to make it a little more legible. You can also use the addition assignment operator to do this with a little more streamlining and to not need the parentheses: total += parseFloat( values[enc] ) || 0; Hope that helps.
Scott C. said: This is a problem with order of operations.... Thanks for looking at this. That was likewise my initial theory, and I tried that fix (both versions) previously -- but unfortunately, I get even more bizarre results from that. E.g., currently I've got that edit in the Sheet Sandbox with test data {1, 2, 3, 4, 5} and am looking at a display total of 731 (!?). Every data change I make causes that display to cumulatively increase (even if I reduce or add a zero value somewhere)... add an extra "1" and now its 747, change that to "2" and now 764, change it back to "0" and now 779, blank it out (so I'm back to {1, 2, 3, 4, 5}) and now 794. Pretty baffled by that behavior. The "total" variable isn't in use anywhere else on on the sheet or worker script.
1676408020

Edited 1676408035
Daniel C.
Sheet Author
Oh, wait, if I move the var total = 0 inside the on loop then it seems to work correctly. Guess I need both of those fixes. Weird that it was working before.
1676412376
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Ah, I didn't notice that. What is going on there is that when it was outside of the on , it was set to 0 when the game was loaded, and then would just increment from there. The order of operations issue was actually masking this cause it would reset it to 0 giving you the appearance that it reset correctly.
Thanks for double-checking that, appreciate it!