Skip to main content
Participant
May 9, 2020 1:07 AM UTC
Answered

Base Cost + Additional Cost Per Unit Beginning with 4th Unit

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.

This topic is closed to new replies. Start a new post to keep the conversation going.
David J. MyersCorrect answer
Participant
May 9, 2020 9:21 PM UTC

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);
}

 

6 replies

David J. MyersAuthorCorrect answer
Participant
May 9, 2020 9:21 PM UTC

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);
}

 

Translate
Bernd Alheit
Community Expert
Community Expert
May 9, 2020 7:21 AM UTC

For the calculation of field "FFCondo" you use the value of field "FFCondo".
Why does you use this recursive calculation?

Translate
Participant
May 9, 2020 3:31 PM UTC

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:

CONDOMINIUM DEPOSIT WORKSHEET

Translate
Bernd Alheit
Community Expert
Community Expert
May 9, 2020 3:39 PM UTC

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

Translate