8 Replies Latest reply on Mar 10, 2015 5:25 AM by defkopta116

# Javascript to divide unevenly

Hey guys,

So I've been working on a form in Adobe Acrobat Pro XI, and I have come up with some things I need to do with Javascript - but I can't quite work out how to do it. Any help would be greatly appreciated!

The slighly curly problem is that I have a box which has a main total, and I then divide that total into three new boxes. Simple enough except that in the case of a number that doesn't evenly divide by 3, I don't want fractions. I want it to divide unevenly, and for it to put the larger number in, say, box 3, then 2 if there's still another one.

I'll give you an example: If the Total box is 28, then I would want the other boxes to be: box 1:8 box 2:8 box 3:9

If the Total box is 29, then I would want the three derivative boxes to be: box 1: 8, box 2: 9, box 3: 9

Hope that makes sense!

The formula I have currently been using is: event.value = ( this.getField("Total Health").value/ 3)

This divides the Total box into thirds, and gives a fraction (so in the case of 28, it gives 9.33333333...)

Cheers!

• ###### 1. Re: Javascript to divide unevenly
It's doable, but your examples don't make sense. 8+8+9=25, not 28. And 8+9+9=26, not 29...

I think the easiest way is to use this code for the first two fields:
event.value = Math.floor(+this.getField("Total Health").value/3);

And this this code for the third one:

```var total = +this.getField("Total Health").value;
event.value = total-(Math.floor(total/3)*2);
```

This will result in 9,9,10 for 28 and 9,9,11 for 29.

• ###### 2. Re: Javascript to divide unevenly

Thanks mate. Yeah you're right, my maths is totally out on those! Yeech.

That formula works just as you said! So that's awesome!!

I may be pushing my luck, but is there any way to make it so those totals end up more like 9,10,10 for 29?

Cheers!

• ###### 3. Re: Javascript to divide unevenly

How about 10, 10, 9? That's easier to implement.

• ###### 4. Re: Javascript to divide unevenly

Yeah I guess that I could work. I'm easy on that front.

• ###### 5. Re: Javascript to divide unevenly

Then replace Math.floor with Math.round in the code above.

• ###### 6. Re: Javascript to divide unevenly

Hey mate, well that certainly was a step in the right direction! You sure know your coding.

For a moment I had an intense air pump moment! haha. And then there was just one little fly in the ointment... which is surprising, as it works so well on everything, up to this one little bit... which is weird.

I replaced the .floor with .round on all those 3 equations, and then I simply reversed the boxes, and it worked as I had needed it to, which is fabulous! What I found happens now is that on, say, 19 Total Health, it will go 7, 6, 6. And then at 20 it will go 6, 7, 7. Bahahaa. So random. I tried it with having the boxes 7, 7, 6, and when I put the total to 19 it still went 6, 6, 7, so it's the same either way.

Curly, but still way better, so thanks heaps already! If you have any ideas for this one, let me know, and I will be most appreciative

• ###### 7. Re: Javascript to divide unevenly

I'm not sure what you "reversed", exactly... For me the result for 19 is 6,6,7 and for 20 it's 7,7,6. Why is that random? It follows the pattern that the first two are the same and the last is what's left. If you want it to work differently then a more complex script is required.

• ###### 8. Re: Javascript to divide unevenly

Ah, I see what you mean. The first two are always the same. Gotcha! That is exactly what it does.