Script for if greater than-type situation

I have EXTREMELY LIMITED Javascript knowledge.

Situation is this (for Adobe/Foxit)

  • Maximum refund for category
  • Program cost
  • Less personal contribution
  1. Total claim request
  2. Maximum allowable

Bulleted items are user-entry
Numbered items are calculation

Total claim request is simple and I don’t need help on that

Formula I need should allow for: Maximum allowable is equal or smaller than Maximum refund for category.

So basically IF Total claim request is =< than Maximum refund for category, then Maximum allowable = Total claim request. ELSE = Maximum refund for category

Hope it is clear… Thanks in advance

Not sure what you are asking here? But if you are asking someone to write some code for you then you probably came to the wrong place.

I’m asking for guidance - if anyone is willing to tell me what the proper coding is, I can take it from there.

I have this so far, based on previous simpler codes I have devised:

var a = getField(“Application amount”);
var b = getField(“Max funding for category”);
var c = getField(“Max funding amount”);

c.value=if a.value=< b.value, a.value

So the c.value is where I am stuck, how to insert a proper formula that does the if/then/else determination.

c.value is the amount that is determined based on whether the actual claim amount - a.value - is equal or smaller than b.value. If it is larger than b.value then c.value is b.value.

In simple English -
You are entitled to $800 maximum funding (b.value)
Your application is for $1,200, for example (a.value)
Your Max Funding Amount is $800 (c.value) because $1,200 is MORE than the $800 max.

Of course an applicant can o this math by hand. I just want to automate it because there’s nothing better than spending oodles of time to save 30 seconds later.

I came up with this solution after some research, trial & error. Let me know if there is a better way. This works perfectly for my purposes:

var A = this.getField(“Amount”).value

var B = this.getField(“Maximum”).value

if( A > B ) event.value = B

else if( B > A ) event.value = A

else event.value = B

Great job! Ya, you could tighten this up a little. You could get rid of that last else statement by changing the condition on the if slightly. And doing that would then allow you to change the current else if to just an else and you wouldn’t even need a second comparison. I’ll give you a hint: In addition to the > and < operators there are also >= and <= operators.

Also, I would recommend you choose better variable names. Instead of A I would probably use amount and for B I would probably use maximum. That way you don’t have to think about what A and B actually represent as you are looking at the code since the variable names will tell you exactly what they are.

I don’t know anything about Adobe/Foxit but I’m assuming (hoping) that getField is actually returning numbers and not strings. Because if it is returning strings then you will get some nasty surprises. I would suggest you test with the Amount set to 90 and the Maximum set to 100 and make sure it comes out correctly.

Thank you for the feedback.

Regarding variables’ names, great tip. It could be the same as the variable itself, makes the flow and thought pattern easier.

It is returning numbers as the entry fields are set to numbers only. I tested it as suggested, and a dozen other ways, appears to be pretty solid. Is there any other way that string vs number could be checked?

Great question. I should have included this in the first place.

Yes, you can use the typeof operator to see what the actual type of a value is.

var A = this.getField("Amount").value;
console.log(typeof A);

The console.log method will print the result of typeof A to the browser’s console and then you would hopefully see “number” instead of “string”. I don’t know about the environment you are working in and so I’m not sure if console.log will work for you? If not then you’d have to figure out another way to see it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.