Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- Home
- >
- Acrobat SDK
- >
- Base Cost + Additional Cost Per Unit Beginning wit...

Community Beginner
,

May 08, 2020

Copy link to clipboard

Copied

I'm struggling to work out a simple script to calculate the total cost after a specific number of units are added, but remain at base cost for the first 3 units.

For instance:

- Cost of 1st Unit = $475 (Base cost.)
- Cost of 1st Unit + 2 Additional Units (Total of 3 Units) = $475 (Same cost as base cost.)
- Cost of 1st Unit + 3 Additional Units (Total of 4 Units) = $525
- Cost of 1st Unit + 4 Additional Units (Total of 5 Units) = $575

etc.

The Excel formula that gives this result is:

`=475+MAX(0,4-3)*50`

Of course, that works in Excel, but not Acrobat.

The JavaScript code I've attempted is:

```
var ffcondo = Number(this.getField("FFCondo").value);
var pgcountcondo = Number(this.getField("PGcountCondo").value);
if (pgcountcondo == "") event.value = "";
else event.value = (ffcondo+(pgcountcondo-3)*50) <= 475 ? 475 : (ffcondo+(pgcountcondo-3)*50);
```

Unfortunately, the results I get with that JavaScript code are unreliable. Sometimes the result is correct; more often it's wrong. I'm very much a JavaScript novice. Attached is the file I'm working with.

Community Beginner

After guidance from Bernd_Alheit and try67, this is the JavaScript I ended up using:

```
var pgcountcondo = Number(this.getField("PGcountCondo").value);
if (pgcountcondo == "") event.value = "";
else {
var v = (475+(pgcountcondo-3)*50);
event.value = Math.max(v, 475);
}
```

TOPICS

Acrobat SDK and JavaScript,
PDF forms

Community Guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Community Beginner
,

May 08, 2020

Copy link to clipboard

Copied

I'm struggling to work out a simple script to calculate the total cost after a specific number of units are added, but remain at base cost for the first 3 units.

For instance:

- Cost of 1st Unit = $475 (Base cost.)
- Cost of 1st Unit + 2 Additional Units (Total of 3 Units) = $475 (Same cost as base cost.)
- Cost of 1st Unit + 3 Additional Units (Total of 4 Units) = $525
- Cost of 1st Unit + 4 Additional Units (Total of 5 Units) = $575

etc.

The Excel formula that gives this result is:

`=475+MAX(0,4-3)*50`

Of course, that works in Excel, but not Acrobat.

The JavaScript code I've attempted is:

```
var ffcondo = Number(this.getField("FFCondo").value);
var pgcountcondo = Number(this.getField("PGcountCondo").value);
if (pgcountcondo == "") event.value = "";
else event.value = (ffcondo+(pgcountcondo-3)*50) <= 475 ? 475 : (ffcondo+(pgcountcondo-3)*50);
```

Unfortunately, the results I get with that JavaScript code are unreliable. Sometimes the result is correct; more often it's wrong. I'm very much a JavaScript novice. Attached is the file I'm working with.

Community Beginner

After guidance from Bernd_Alheit and try67, this is the JavaScript I ended up using:

```
var pgcountcondo = Number(this.getField("PGcountCondo").value);
if (pgcountcondo == "") event.value = "";
else {
var v = (475+(pgcountcondo-3)*50);
event.value = Math.max(v, 475);
}
```

TOPICS

Acrobat SDK and JavaScript,
PDF forms

Community Guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

David J. Myers

May 08, 2020
0
/t5/acrobat-sdk/base-cost-additional-cost-per-unit-beginning-with-4th-unit/td-p/11116936
6
Replies
6

Adobe Community Professional
,

May 09, 2020

Copy link to clipboard

Copied

Why does you use this recursive calculation?

Community Guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Reply

Loading...

Bernd Alheit

May 09, 2020
0
/t5/acrobat-sdk/base-cost-additional-cost-per-unit-beginning-with-4th-unit/m-p/11117258#M5846
Community Beginner
,

May 09, 2020

Copy link to clipboard

Copied

Bernd:

I'm not entirely sure what you're asking. I normally don't write much JavaScript, and therefore I'm a novice in this area.

Perhaps linking to a Google Sheet that shows explicitly what I'm attempting to do will help:

Community Guidelines

Reply

Loading...

David J. Myers

May 09, 2020
0
/t5/acrobat-sdk/base-cost-additional-cost-per-unit-beginning-with-4th-unit/m-p/11118024#M5847
Adobe Community Professional
,

May 09, 2020

Copy link to clipboard

Copied

Why does you use for the calculation of field "FFCondo" the value of field "FFCondo"?

Community Guidelines

Reply

Loading...

Bernd Alheit

May 09, 2020
1
/t5/acrobat-sdk/base-cost-additional-cost-per-unit-beginning-with-4th-unit/m-p/11118045#M5848
Community Beginner
,

May 09, 2020

Copy link to clipboard

Copied

Bernd: Thank you for clarifying that.

Here's what I changed the JS in FFCondo to:

```
var ffcondo = Number(this.getField("FFCondo").value);
var pgcountcondo = Number(this.getField("PGcountCondo").value);
if (pgcountcondo == "") event.value = "";
else event.value = (475+(pgcountcondo-3)*50) <= 475 ? 475 : (475+(pgcountcondo-3)*50);
```

So far, it seems to calculate results correctly. I haven't tested it exhaustively yet, but it looks promising.

Thank you!

Community Guidelines

Reply

Loading...

David J. Myers

May 09, 2020
0
/t5/acrobat-sdk/base-cost-additional-cost-per-unit-beginning-with-4th-unit/m-p/11118227#M5849
Most Valuable Participant
,

May 09, 2020

Copy link to clipboard

Copied

That last line is quite clanky. I would replace it with this:

else {

var v = (475+(pgcountcondo-3)*50);

event.value = Math.max(v, 475);

}

Community Guidelines

Reply

Loading...

try67

May 09, 2020
1
/t5/acrobat-sdk/base-cost-additional-cost-per-unit-beginning-with-4th-unit/m-p/11118497#M5850
Community Beginner
,

May 09, 2020

Copy link to clipboard

Copied

After guidance from Bernd_Alheit and try67, this is the JavaScript I ended up using:

```
var pgcountcondo = Number(this.getField("PGcountCondo").value);
if (pgcountcondo == "") event.value = "";
else {
var v = (475+(pgcountcondo-3)*50);
event.value = Math.max(v, 475);
}
```

Community Guidelines

Reply

Loading...

David J. Myers

May 09, 2020
0
/t5/acrobat-sdk/base-cost-additional-cost-per-unit-beginning-with-4th-unit/m-p/11118615#M5851
Using the Community
Experience League
Terms of Use
Privacy Policy
Cookie preferences
AdChoices
Language:

- Deutsch
- English
- Español
- Français
- 日本語コミュニティ
- Português

Copyright © 2020 Adobe. All rights reserved.