I'm trying to create a form with:
- a series of numerators and denominators to calculate
- if the field is left blank, it is not included in the calculation
- the calculation to be updated in real time, and not wait for each field to have a number in it before it calculates.
This is the only calculation in the form, so changing the calculation order is not used. In the document javascript, I have
this.calculateNow();
and in the custom calculation script for the field, I have
(function () {
var v1 = +getField("Text1").value;
var v2 = +getField("Text2").value;
var v3 = +getField("Text3").value;
var v4 = +getField("Text4").value;
var v5 = +getField("Text5").value;
var v6 = +getField("Text6").value;
var v7 = +getField("Text7").value;
var v8 = +getField("Text8").value;
var v9 = +getField("Text9").value;
var v10 = +getField("Text10").value;
event.value = (v2 * v4 * v6 * v8 * v10) !== 0 ? ((v1 * v3 * v5 * v7 * v9) / (v2 * v4 * v6 * v8 * v10)) : "";
})();
The issues I'm having:
- that the calculation does not calculate in real time
- all fields still have to be entered for the calculation to occur.
- if zero is entered, the calculation will not run.
Your code is a bit repetitive. You may use an array containing the field ids:
Then we can map the fields to their value and use the or operator to fill unset fields:
That array then can be reduced to a certain value, e.g. with mutliplication: