Skip to main content
Participating Frequently
Jun 29, 2017 8:47 AM UTC
Answered

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.

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Jun 29, 2017 9:28 AM UTC

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

6 replies

jaimec77Author
Participating Frequently
Jun 30, 2017 11:32 PM UTC

Thank you!

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Jun 29, 2017 9:28 AM UTC

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

Translate
jaimec77Author
Participating Frequently
Jun 29, 2017 10:18 AM UTC

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

Translate
try67
Community Expert
Community Expert
Jun 29, 2017 11:42 AM UTC

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.

Translate