Skip to main content
Participant
November 7, 2019
Answered

How to calculate using variables in javascript?

  • November 7, 2019
  • 1 reply
  • 2176 views

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.

This topic has been closed for replies.
Correct answer try67

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;

 

1 reply

try67
Community Expert
Community Expert
November 7, 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.

Participant
November 8, 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.  

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
November 8, 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;