Skip to main content
Participating Frequently
June 29, 2017
Answered

Calculate based on Age

  • June 29, 2017
  • 2 replies
  • 788 views

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.

This topic has been closed for replies.
Correct answer try67

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 replies

jaimec77Author
Participating Frequently
June 30, 2017

Thank you!

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 29, 2017

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);

jaimec77Author
Participating Frequently
June 29, 2017

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);

try67
Community Expert
Community Expert
June 29, 2017

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.