6 Replies Latest reply on Jun 30, 2017 4:32 PM by jaimec77

# Calculate based on Age

Hello, I need help with a form i am putting together and am very new to javascript.

If the age entered in box1 is 70, i would like the value in box4 = "Too Old" or "0" and if age is 15, then box4 = "Too Young" or "0". If the age is between 15&70, then calculate box2.value multiplied box3.value.

Basically, in MS Excel, my formula is =IF(OR(Cell1>70,"Too Old",Cell1<15,"Too Young",Cell2*Cell3)

Thank you.

• ###### 1. Re: Calculate based on Age

Use this code as the custom calculation script of box4:

```var age = Number(this.getField("box1").value);
if (age>70) event.value = "Too old";
else if (age<15) event.value = "Too young";
else event.value = Number(this.getField("box2").value) * Number(this.getField("box3").value);
```
• ###### 2. Re: Calculate based on Age

Thank you!!! It works!

Now, I have another question.

this is the formula I want to achieve:

=IF(DOQ-DJC>45,0,IF(VLCR>MIN(300000,CLC*3,SAL*8),MIN(300000,CLC*3,SAL*8),VLCR))

i have tried to manipulate the code you have written earlier. But, no joy.

var sal = Number(this.getField("SAL").value;

var age = Number(this.getField("ANB").value);

var djc = Number(this.getField("DJC").value);

var doq = Number(this.getField("DOQ").value);

var clc = Number(this.getField("CLC").value);

var vlcr = Number(this.getField("VLCR").value);

if (age>70) event.value = "0";

else if (age<15) event.value = "0";

else if (djc - doq)>45 ="0";

else if (VLCR>300000 || VLCR>CLC*3 || VLCR>SAL*8);

• ###### 3. Re: Calculate based on Age

You have some mistakes in your code. What should be the field's value if the last condition is true, or if none of them is met?

if (age>70) event.value = "0";

else if (age<15) event.value = "0";

else if ((djc - doq)>45) event.value = "0";

else if (vlcr>300000 || vlcr>(clc*3) || vlcr>(sal*8)) event.value = "???";

else event.value = "????";

Edit: Also, JS is case-sensitive, so make sure you spell the names of your variables consistently.

• ###### 4. Re: Calculate based on Age

else if (vlcr>300000 || vlcr>(clc*3) || vlcr>(sal*8)) event.value = the minimum or lesser value of the 3;

else event.value = (vlcr);

• ###### 5. Re: Calculate based on Age

event.value = Math.min(vlcr, clc, sal);