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

How to calculate using variables in javascript?

Community Beginner ,
Nov 07, 2019 Nov 07, 2019

Hello, I need help trying to figure out how to create a code in a pdf from with javascript where I want to be able to, using 2 different text variables, get a narrow result and then multiply it. For example: if "name" = "john" and "sex" = "male", that set of variables gives me a value ex: "6", after that I would get another value presented in a editable field and multiply it by the result in my search.

Please check the code I came up with:

var a = this.getField("peso01").value;
var b = this.getField("uni01").value;
var c = this.getField("quant01").value;
var d = 6;
var e = 240;
var result;
switch (a) {
case '1':
    a = "2KG";
    break;
case '2':
    b = "PAL";
    break;
case '3':
    b = "PACK";
    if (a === "2KG" && b === "PACK") {
        result = c * d;
    } else if (a === "2KG" && b === "PAL") {
        result = c * e;
    }
    break;
}

I can't seem to make it work, please help me get it right.

1.9K
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
Community Expert ,
Nov 08, 2019 Nov 08, 2019

You can use this code as the custom calculation script of your field to achieve what you described above (I used 5 as the multiplier. You can of course change it to something else...):

 

if (this.getField("peso01").valueAsString=="2KG" && this.getField("uni01").valueAsString=="PAL")

event.value = Number(this.getField("quant01").valueAsString) * 5;

 

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 ,
Nov 07, 2019 Nov 07, 2019

Your code is very strange. Why are you converting values from one thing to another? Why are you using a switch statement?

Try to explain in detail what you want to achieve and we could help you write proper code to do it.

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 ,
Nov 08, 2019 Nov 08, 2019

Hi, i do not have much experience with javascript, so I built what I thought was right while looking at stuff online. Thank you for you response.

What I'm trying to do is (using the terms I used in my code), when the value of peso01 equals 2KG ( I have more than just this weight) and the value of uni01 equals PAL( here I have 2 types of packaging), i want to get the value in quant01 (wich is inserted by the client) and then multiply it by a number. After that I can manage to do the rest I think.

What I'm trying to do is build a pdf form where the client choses a weight and the type of the packaging and I multiply the quantity he/she wants by the number of product the packaging can take.  

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 ,
Nov 08, 2019 Nov 08, 2019

You can use this code as the custom calculation script of your field to achieve what you described above (I used 5 as the multiplier. You can of course change it to something else...):

 

if (this.getField("peso01").valueAsString=="2KG" && this.getField("uni01").valueAsString=="PAL")

event.value = Number(this.getField("quant01").valueAsString) * 5;

 

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 ,
Nov 08, 2019 Nov 08, 2019

Thank you for the response. 

It isn't working for me, I applied the code you gave to me but the field doesn't show the value like it's suposed to.

Here's an capture of one of the lines from de form for further comprehention.

1.PNGexpand image

As you can see, the fied "peso01"is set as 2KG, "uni01" as PAL and I gave it the quantity of 20 and it doesn't 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 Expert ,
Nov 08, 2019 Nov 08, 2019

Press Ctrl+J and check if there are error messages in the console window that opens.

Or share the file with us, so we can see for ourselves what's going on.

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 ,
Nov 08, 2019 Nov 08, 2019
LATEST

It worked afterall, i inserted the code in the wrong place.

Thank you!!

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