Calculating premium from a rate chart I have to calculate the premium from a rate chart based on a person's age and the amount of insurance they request. I started out using if/switch statements, but the code is getting too long and seems pretty redundant.

var premium;
var beneAmt = $('#PrimaryApplicantTTLBenefitAmountInput').val();
var age = $('#PrimaryApplicantAge').val();

if (50000 &lt;= beneAmt &lt; 75000) {

    switch (true) {

        case (age &lt; 30):

            premium = 2.00;
            break;

        case (30 &lt;= age &lt; 40):

            premium = 2.50;
            break;

        case (40 &lt;= age &lt; 50):

            premium = 5.00;
            break;

        case (50 &lt;= age &lt; 60):

            premium = 15.00;
            break;

        case (60 &lt;= age &lt; 65):

            premium = 21.50;
            break;

        case (65 &lt;= age &lt; 70):

            premium = 20.80;
            break;

        case (70 &lt;= age &lt; 75):

            premium = 23.25;
            break;

        case (age &gt;= 75):

            premium = 35.00;
            break;

    }

} else if (75000 &lt;= beneAmt &lt; 100000) {

    switch (true) {

        case (age &lt; 30):

            premium = 3.00;
            break;

        case (30 &lt;= age &lt; 40):

            premium = 3.75;
            break;

        case (40 &lt;= age &lt; 50):

            premium = 7.50;
            break;

        case (50 &lt;= age &lt; 60):

            premium = 22.50;
            break;

        case (60 &lt;= age &lt; 65):

            premium = 32.25;
            break;

        case (65 &lt;= age &lt; 70):

            premium = 31.20;
            break;

        case (70 &lt;= age &lt; 75):

            premium = 34.88;
            break;

        case (age &gt;= 75):

            premium = 52.50;
            break;

    }

} else if (100000 &lt;= beneAmt &lt; 125000) {

    switch (true) {

        case (age &lt; 30):

            premium = 4.00;
            break;

        case (30 &lt;= age &lt; 40):

            premium = 5.00;
            break;

        case (40 &lt;= age &lt; 50):

            premium = 10.00;
            break;

        case (50 &lt;= age &lt; 60):

            premium = 30.00;
            break;

        case (60 &lt;= age &lt; 65):

            premium = 43.00;
            break;

        case (65 &lt;= age &lt; 70):

            premium = 41.60;
            break;

        case (70 &lt;= age &lt; 75):

            premium = 46.50;
            break;

        case (age &gt;= 75):

            premium = 70.00;
            break;

    }

}


How else could I go about this?
I think the main improvement to this code would be to separate the definition of the premium lookup table from the logic that finds the premium for a given benefit and age, for example, using an Object-Oriented design.  Doing so will simplify changes to the table and make the code easier to test and debug.

For example, consider the usage below that makes the table itself an object mapping benefit and age ranges to premium values:

var premiumTable = new PremiumTable({
  '[50000, 75000)': {  // 50k &lt;= benefit &lt; 75k
    '     30)':  2.00, //          age &lt;  30
    '[30, 40)':  2.50, //    30 &lt;= age &lt;= 40
    '[40, 50)':  5.00, //    40 &lt;= age &lt;= 50
    // ...
    '[75     ': 35.00, //    75 &gt;= age
  },
  '[75000, 100000)': {
    // ...
  },
  // ...
});

var premium = premiumTable.valueForBenefitAndAge(someBenefit, someAge);


Now you can implement the valueForBenefitAndAge method in a way that can be unit tested and changes to the premium table are localized to the initializer object rather than being related directly to the code itself.

Moreover, the PremiumTable constructor function can validate the premium table input object based on rules that will be easier to encode than using your example code.  Additionally, the table could be loaded as a separate resource to further isolate the code that applies the logic from the premium table values themselves.