Expand my Community achievements bar.

Pass a 'for' variable to reference dynamic binding names

Avatar

Level 1

I just started learning FormCalc to help an enduser and came up stuck.

Here's the general form:

Multiple checkboxes,

(name / value when checked)

box1 / 1

box2 / 2

box3 / 3

A numeric field below these 3 boxes needs to calculate the values of those boxes that are checked, then divide by the number of checked boxes

So, if all three boxes are checked, the numeric field would calculate (1 +2 +3) / 3

There are other checkboxes on the form that are not included in this calculation.

I can do multiple if statements :

var total = 0;

if (box1>0) then

  total=total+1;

endif

if (box2>0) then

  total=total+1;

endif

if (box3>0) then

  total=total+1;

endif

if (Eval(total)>0) then

  (box1 + box2 + box3) / Eval(total)

endif

I'd like to simplify it with a for statement :

var total = 0;

for a=1 upto 3 do

  if (box[a]>0) then

    total=total+1;

  endif

endfor

if (Eval(total)>0) then

  (box1 + box2 + box3) / Eval(total)

endif

But I end up with an error "accessor 'box[1]' is unknown".  Which makes sense.  The statement is passing the variable, but including the brackets.  What am I missing?

TIA

2 Replies

Avatar

Level 10

var sum = 0;

var details = this.resolveNodes("po.box[*]");//po.box[*] returns all the objects with the name "box" in the subform "po"

for (var i = 0; i < details.length; i++) {

    sum += details.item(i).rawValue;

}

you can do the same with FormCalc

Avatar

Level 1

Yes, I was looking for FormCalc, not Jscript.  But thanks.

Figured it out:

Changed all the checkbox object names to 'box'.  In the Hierarchy window, it renames the objects box[1], box[2], box[3].

Now the for statement works

var total = 0;

for a=1 upto 3 do

  if (box[a]>0) then

    total=total+1;

  endif

endfor