Skip to main content
robd64027506
Known Participant
February 28, 2017
Answered

Add numbers in Javascript

  • February 28, 2017
  • 2 replies
  • 606 views

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 has been closed for replies.
Correct answer try67

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
March 1, 2017

That did the trick, thank you very much!

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
February 28, 2017

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.