8 Replies Latest reply on Sep 29, 2016 6:15 AM by rsbisa

    Complicated nested IF statement instructions needed

    rsbisa Level 1

      Hello! I'm completely new to forms and JavaScript. I'm struggling to recreate JavaScript IF/THEN statement that is to calculate the following in a single form field "Employer Match" (formatted as Number, Currency, US dollar sign). Employer Match gets its values from two user input fields, "Annual Income" (formatted Number, Currency, US Dollar sign), and, "401k Contribution" (format Number, Currency, US Dollar sign; validation 0 to 18000).

       

      Here's the challenge:

      Employer Match = Equal to $1 of every $1 of 401k Contribution up to 3% of Annual Income and .5 (50 cents) of every $1 of up to the remainder 2% of Annual Income.

       

      I'm able to do this easy enough in Excel. But, JavaScript, not so much.

       

      So,

       

      1. Into Annual Income form field, User types their annual income, say, $80,000
      2. Into 401k Contribution form field, user types an amount, say, $6,000 (or, 7.5% of Income); or for our purposes any other amount greater than 3% of Income, but less than $18,000
      3. The calculation in Employer Match field needs to determine if 401k Contribution is greater than 3% of Income or not.
        1. If 401k Contribution < (Annual Income * .03), then Employer Match = 401k Contribution
        2. If 401K Contribution > (Annual Income * .03), then Employer Match = (((401k Contribution - (Annual Income * .03)) + (Annual Income * .02) * .5))

       

      In the above instance, of the $6,000 401k Contribution, $2,400 (3% of Annual Income) would be matched $1 to $1, and of the remainder $3,600, only $1,600 (2% of Annual Income) would be matched at .50 per $1 (or, $800). So, Employer Match = $3,200. Note, that if user had typed, say, $3,000 into 401k Contribution, where $2,400 is 3% of Annual Income, the remainder $600 would be matched at .50 cents, or $300. So, Employer Match = $2,400 + 300, or $2,700.

       

      I'm looking for a JavaScript complex calculation that does the above. Hope this makes sense.

       

      Any help greatly appreciated.