Skip to main content
Participating Frequently
January 19, 2024
Question

JavaScript Calculation Custom (100,000 - 0)/100,000*100 should equal 100 but I am getting blank

  • January 19, 2024
  • 2 replies
  • 1708 views

I am new to JavaScript and all of the fields work with this JavaScript except when I get to $0.00 in the f2 field and then it leaves the field blank.  I want it to show 100.  What do I need to add to the Script?

 

It is basically Loan Amount minus Balance divided by loan amount multiplied by 100 equals percent of loan

 

var f1 = Number(this.getField("LoanAmt").valueAsString);
var f2 = Number(this.getField("Bal5").valueAsString);
var f3 = Number(this.getField("Pct5").valueAsString);

if(f2)
event.value = (f1-f2)/f1*100;
else
event.value = "";

 

 

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
January 19, 2024

The problem is you can't tell in your code when "Bal5" is empty, and when it's zero.

Try this:

 

var f1 = Number(this.getField("LoanAmt").valueAsString);
var f2 = this.getField("Bal5").valueAsString;

if (f2=="") event.value = "";
else {
	f2 = Number(f2);
	event.value = (f1-f2)/f1*100;
}

 

(I removed f3 which wasn't used)

Participating Frequently
January 22, 2024

I put in the following and still got a blank field

 

var f1 = Number(this.getField("LoanAmt").valueAsString);
var f2 = Number(this.getField("Bal5").valueAsString);
var f3 = Number(this.getField("Pct5").valueAsString);

if (f2=="") event.value = "";
else {f2 = Number(f2);event.value = (f1-f2)/f1*100;}

 

 

try67
Community Expert
Community Expert
January 22, 2024

Use the code as I provided it.

Nesa Nurani
Community Expert
Community Expert
January 19, 2024

To show 100 instead of blank, replace event.value = ""; with: event.value = 100;

Participating Frequently
January 19, 2024

when I replaced it with event.value=100; it leaves 100 in the field even if f2 is blank.  I only want it to show 100 if the (LoanAmt - Bal5)/LoanAmt*100 = 100

Bal5 changes every field as you go down.  

 

 

 

Nesa Nurani
Community Expert
Community Expert
January 19, 2024

If you want to show 100 only if the calculation result is 100 then use something like this:

var f1 = Number(this.getField("LoanAmt").valueAsString);
var f2 = Number(this.getField("Bal5").valueAsString);

if(f1 !== 0)
var t = (f1-f2)/f1*100;

if(t === 100)
event.value = t;
else
event.value = "";