Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
9

If/then statement with division

Community Beginner ,
Oct 09, 2023 Oct 09, 2023

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.

TOPICS
JavaScript , PDF , PDF forms
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2023 Oct 09, 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.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 10, 2023 Oct 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2023 Oct 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 = "";

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2023 Oct 09, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2023 Oct 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 10, 2023 Oct 10, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2023 Oct 10, 2023

At the end of the code add this:

 

else event.value = "";

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 10, 2023 Oct 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2023 Oct 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 = "";
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 10, 2023 Oct 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.  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2023 Oct 10, 2023

Does you use a script at the checkbox?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 11, 2023 Oct 11, 2023

Script in Text3

 

if (this.getField("CheckBox2").value != "Off") {
// box is checked
event.value = 2;
}
else {
// box is unchecked
event.value = "";
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 11, 2023 Oct 11, 2023

Change the field calculation order. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 12, 2023 Oct 12, 2023

Thank you.  I've tried, but I can't seem to get it right.  I hate to be so needy, but could you please post how the script should look?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 12, 2023 Oct 12, 2023
LATEST

The script is ok. Change the field calculation order.

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