Skip to main content
Known Participant
February 24, 2019
Question

Why is my for loop iterating only once and not calculating fields correctly?

  • February 24, 2019
  • 3 replies
  • 1118 views

Hello,

I'm trying to create a script which will calculate the proportional percentage of a numerical entry sectioned by date. For example:

Date%Adj. %Total
2/1/195%100%5
2/2/195%100%5

Date%Adj. %Total
2/15/19511.11%45
1022.22%
3066.66%
2/18/19675%8
225%


So, the "Adj. %" values associated with each "parent" "Date" value should equal to 100% when added together. My issue is that my script doesn't seem to iterate properly and I end up with strange "Adj. %" values that do not reflect the respective "Date" and "%" field value entries. I included a link to a pdf, which includes the script I'm having issues with: https://docdro.id/TDcy6RX


I'm going crazy over this, any help will be highly appreciated!

This topic has been closed for replies.

3 replies

try67
Community Expert
Community Expert
February 24, 2019

I suggest you re-think about how this should work. Instead of keeping the date fields empty I think you should make sure they all have a value, and then iterate over the fields with the same value, add them up and then iterate over them again to calculate the relative value of each one from the total.

aerynsAuthor
Known Participant
February 25, 2019

I tried rewriting my script to follow the logic you outlined, but it ends up calculating the correct "Adj %" for only the first and last rows that share the same date value, do you know what is causing this issue?:

Also, is there a way I can copy the formatted script into my replies(so I don't have to post a pic)? When I try to control-c it, it pastes without the formatting?

try67
Community Expert
Community Expert
February 25, 2019

You must post your code as text. It's nearly impossible to debug it like that.

After pasting it into a new reply click on Use Advanced Editor (at the top-right) of the reply window, then select the code and click the ">>" icon at the top.

Then select "Syntax highlighting" and "JavaScript".

Even if you don't have access to this option a plain-text code (without formatting) is better than an image.

Bernd Alheit
Community Expert
Community Expert
February 24, 2019

Why does you use following else part:

} else {

totalArray.value = "";

adjPerArray.value = "";

};

aerynsAuthor
Known Participant
February 24, 2019

That else statement is supposed to clear/reset the fields for a specific row "i" if there isn't at least one row above it that has both "Date" and "%" fields with values entered. In practice, I don't think my script is executing in said way and I can't figure out why.

try67
Community Expert
Community Expert
February 24, 2019

There are errors in your code. They appear as soon as the file is opened.

Go to Edit - Preferences - JavaScript and tick the option to show the console on errors and messages. This is a must-have setting for developers.

aerynsAuthor
Known Participant
February 24, 2019

I've gone ahead and tinkered with my code in the console a bit and have been able to get both my for loops to iterate successfully and console print the results( ie. 0,1,2,3). This means that the error in my code is in the if statements, correct? If so, would you be able to point me in the right direction from here? When I isolate the if statements in the console log, I get "SyntaxError: syntax error". I've gone over the syntax of my code and it all looks fine to me so I am not sure how or if this is the root problem of my faulty script.

Also, I get the following errors when I first open the pdf, could they be contributing to my issues?:

try67
Community Expert
Community Expert
February 24, 2019

Post the new full code, as well as the full error message.

It might not cause it, but it's certainly not helping.

That message means one of the field names you defined in the array is incorrect.