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

Answer return as blank when input values are zero?

Explorer ,
Sep 20, 2023 Sep 20, 2023

Hi there, I have this problem when a cell has value of zero, then the answer returned is blank. 
I am not sure how to resolve this problem. 

For example: 

kennethkamchuh21426993_0-1695262492770.png

In the above image, I have applied the code below but it returns the answer as blank. 

I expected the answer to be 1.1

which can be calculated by: 

(top - bkg) / ((top-bkg)+(bottom-bkg)) *100 

(0.1-0)/((0.1-0)+(9-0))*100 

= 1.1

 

 

var v1 = Number(this.getField("Top").valueAsString);
var v2 = Number(this.getField("Bottom").valueAsString);
var v3 =
Number(this.getField("Bkg").valueAsString);

if(v3){
 if(v1=="" || v2=="") event.value = "";else
 event.value = (v1-v3)/((v2-v3)+(v1-v3))*100;}
else 
event.value = "";

 

Any idea what's wrong with my code? Thanks

 

TOPICS
JavaScript , PDF forms
917
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
1 ACCEPTED SOLUTION
Contributor ,
Sep 20, 2023 Sep 20, 2023

Hi @kenneth kam chuh21426993 

You can replace your code with the following code, which should work correctly:

 

var topValue = Number(this.getField("Top").value);
var bottomValue = Number(this.getField("Bottom").value);
var bkgValue = Number(this.getField("Bkg").value);

if (isNaN(topValue) || isNaN(bottomValue) || isNaN(bkgValue)) {
  event.value = "";
} else if (topValue === 0 && bkgValue === 0) {
  event.value = "";
} else {
  var numerator = topValue - bkgValue;
  var denominator = (topValue - bkgValue) + (bottomValue - bkgValue);

  if (denominator === 0) {
    event.value = "";
  } else {
    event.value = (numerator / denominator) * 100;
  }
}

 

View solution in original post

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 ,
Sep 20, 2023 Sep 20, 2023

I think you mixed fields and variables, you sure v3 should be Bkg field?

Because v3 is 0 (Bkg) so it sets the field empty.

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
Contributor ,
Sep 20, 2023 Sep 20, 2023

Hi @kenneth kam chuh21426993 

You can replace your code with the following code, which should work correctly:

 

var topValue = Number(this.getField("Top").value);
var bottomValue = Number(this.getField("Bottom").value);
var bkgValue = Number(this.getField("Bkg").value);

if (isNaN(topValue) || isNaN(bottomValue) || isNaN(bkgValue)) {
  event.value = "";
} else if (topValue === 0 && bkgValue === 0) {
  event.value = "";
} else {
  var numerator = topValue - bkgValue;
  var denominator = (topValue - bkgValue) + (bottomValue - bkgValue);

  if (denominator === 0) {
    event.value = "";
  } else {
    event.value = (numerator / denominator) * 100;
  }
}

 

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
Explorer ,
Sep 21, 2023 Sep 21, 2023

Thanks heaps for this. It works like a champ. 

Can you briefly explain what's wrong with my code? 

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
Enthusiast ,
Sep 21, 2023 Sep 21, 2023

As Nesa already point out, in your script:

if(v3){
...your script}
else
event.value = "";

Well, v3 is 0 (Bkg) in your example, so it triggers 'else' part and set value to "".

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
Enthusiast ,
Sep 21, 2023 Sep 21, 2023

@kenneth kam chuh21426993the script posted by @Saher Naji will not work correctly.

If you input (Top) 0.1 and (Bottom) 9 it will calculate 1.1 even if (Bkg) is blank, shouldn't (Bkg) be 0?

If you first input (Top) 0.1 while other fields are blank, it will calculate 100.

It may give you unwanted behavior.

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
Contributor ,
Sep 21, 2023 Sep 21, 2023
LATEST

@kenneth kam chuh21426993 

  1. In your code, you are checking if v3 is truthy using if (v3). This means that if the "Bkg" field has any non-empty value (including 0), it will proceed with the calculation. However, your expected behavior is that if "Bkg" is 0, it should still perform the calculation.
  2. In your code, you are using v1 == "" and v2 == "" to check if the "Top" and "Bottom" fields are empty, but it's better to check for isNaN(v1) and isNaN(v2) to ensure they are valid numbers.
  3. The division by zero issue is not handled properly in your code. If denominator is 0, it should return an empty string (or handle it in a way that makes sense for your application), but in your code, it doesn't account for this 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