Skip to main content
robd64027506
Known Participant
Feb 28, 2017 9:21 PM UTC
Answered

Add numbers in Javascript

I have a series of numbers that I need to add, they are in 9 fields. The page is a template, the first problem was getting it change the names of the fields to match the spawned page within the script. I solved that problem.

The current problem is that if one of the fields is blank, it does not read that field as a '0', it stops adding and combines the rest of the numbers as if they were text.

So, if the first 3 fields were 1,1,2 the result would be '4', however if they were 1,1, (blank),2 I would get '22'. It adds the first 2 numbers, then it hits the blank, then it will combine the numbers thereafter.  1,1,(blank),2,3,4 would be 2234, I want it to = 11

I need it to consider a blank field a zero so that it continues to add, the 'custom calculation  script' is below, please help.

(function () {var f_name = event.target.name; var aName = f_name.split("."); var f_prefix = aName[0] + "." + aName[1];

event.value =

this.getField([f_prefix] + ".ADA A QTY").value

+

this.getField([f_prefix] + ".ADA B QTY").value

+

this.getField([f_prefix] + ".ADA C QTY").value

+

this.getField([f_prefix] + ".ADA D QTY").value

+

this.getField([f_prefix] + ".ADA E QTY").value

+

this.getField([f_prefix] + ".ADA F QTY").value

+

this.getField([f_prefix] + ".ADA G QTY").value

+

this.getField([f_prefix] + ".ADA H QTY").value

+

this.getField([f_prefix] + ".ADA I QTY").value

})();

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Feb 28, 2017 9:29 PM UTC

You need to explicitly convert each value to a number. For example:

Number(this.getField(f_prefix + ".ADA A QTY").value)

I also removed the square brackets around f_prefix. They are not needed and can cause the script to fail.

2 replies

robd64027506
Known Participant
Mar 1, 2017 1:50 PM UTC

That did the trick, thank you very much!

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Feb 28, 2017 9:29 PM UTC

You need to explicitly convert each value to a number. For example:

Number(this.getField(f_prefix + ".ADA A QTY").value)

I also removed the square brackets around f_prefix. They are not needed and can cause the script to fail.

Translate