Skip to main content
Participating Frequently
October 10, 2023
Question

If/then statement with division

  • October 10, 2023
  • 4 replies
  • 2839 views

I am trying to write a code that looks at a field and if it equals a specific number (in this case "2"), then it divides another field by that number.  Here is what I have so far...

 

if (this.getField("Tex3").value != 2) {
var numerator = +getField("text1").value;
var denominator = +getField("text2").value;}
// Calculate and set this field value
if (denominator !== 0)
{
event.value = numerator / denominator;
}
else {
event.value = "";
}

 

It is currently returning nothing.

This topic has been closed for replies.

4 replies

Participating Frequently
October 10, 2023

UPDATED CODE:
if (this.getField("Text3").value == 2)
else {
var numerator = +getField("undefined_10").value;
var denominator = +getField("Text3").value;}
// Calculate and set this field value
if (denominator == 0)
{
event.value = numerator / denominator;
}
else {
event.value = "";
}
}

 

Now I am getting a syntax error 2: at line 3

try67
Community Expert
Community Expert
October 10, 2023

This still has a lot of the issues we described... Try this:

 

if (Number(this.getField("Text3").value) == 2) {
	var numerator = Number(this.getField("undefined_10").value);
    var denominator = Number(this.getField("Text3").value);
	if (denominator == 0)
		event.value = "";
    else event.value = numerator / denominator;
} else {
	event.value = "";
}
Participating Frequently
October 10, 2023

Thank you.  I have tried this, but I am still not getting the desired results. 

For further information, the "Text3" field is populated by default as blank or, if a check box is checked, populated with the number 2.  The check box portion is working fine, but when the box is checked and I am getting a result of blank, but when the check box is UNchecked, I am getting the result that I actually need.  I don't know if the check box is causing an issue.  I've attached a screenshot of the project that will hopefully help.

 

I appreciate your help.  

try67
Community Expert
Community Expert
October 10, 2023

In addition to the correct comments made above, you need to define what should happen if the value of the first field is not 2. If you don't do anything then the current value will remain as it was, which is probably not what you want to happen, unless you want the user to be able to fill in a value manually in that case.

Participating Frequently
October 10, 2023

I would like it to return the field as blank.  How do I accomplish this?

try67
Community Expert
Community Expert
October 10, 2023

At the end of the code add this:

 

else event.value = "";

Bernd Alheit
Community Expert
Community Expert
October 10, 2023

Also replace "!=" with "==".

Nesa Nurani
Community Expert
Community Expert
October 10, 2023

Is "Tex3" the correct field name?

Initialize variables outside 'if' statement, or it will throw an error when 'if' condition is not met, and it tries to calculate.

 

Participating Frequently
October 10, 2023

My apologies.  The field name should be "Text3".  I caught that and corrected it right after I posted (of course), but my results were the same.

Can you explain or give an example of what you mean by "Initialize variables outside 'if' statement"?  I'm terribly new to all of this.

Nesa Nurani
Community Expert
Community Expert
October 10, 2023

Pay attention to field name because "Text1" and "text1" are not same.

Use this:

 

var t1 = Number(this.getField("text1").valueAsString);
var t2 = Number(this.getField("text2").valueAsString);
var t3 = Number(this.getField("Text3").valueAsString);

if(t3 === 2){
 if(t2 !== 0)
  event.value = t1/t2;
 else
  event.value = "";}
else
 event.value = "";