Improving or replacing a long if statement I have a an if statement that works just fine, however, it has become a bit long and I need help to figure out how I can reduce the lines of code and thereby the readability.
I have a defaultMonth and set of months and a corresponding price. I need to add or reduce the price depending on the defaultMonth and month selected by the user.
Here are some examples of the output I need (note the interval is the same):
If default month is 60 - set price accordingly:
Month    -     Price
60             0.0
48             0.1
36             0.2
24             0.35
12             0.5

If default month is 48 - set price accordingly:
Month    -     Price
60             -0.1
48             0.0
36             0.1
24             0.25
12             0.4

If default month is 36 - set price accordingly:
Month    -     Price
60             -0.2
48             -0.1
36             0.0
24             0.15
12             0.3

The way I've built it now is with an if statement for each default month and nested ifs for each month.
if (defaultMonth === 60) {
    if (month === 60) {
        this.price += 0.0
    }
    if (month === 48) {
        this.price += 0.1
    }
    if (month === 36) {
        this.price += 0.2
    }
    if (month === 24) {
        this.price += 0.35
    }
    if (month === 12) {
        this.price += 0.5
    }
}
if (defaultMonth === 48) {
    if (month === 60) {
        this.price -= 0.1
    }
    if (month === 48) {
        this.price += 0.0
    }
    if (month === 36) {
        this.price += 0.1
    }
    if (month === 24) {
        this.price += 0.25
    }
    if (month === 12) {
        this.price += 0.4
    }
}
if (defaultMonth === 36) {
    if (month === 60) {
        this.price -= 0.2
    }
    if (month === 48) {
        this.price -= 0.1
    }
    if (month === 36) {
        this.price += 0.0
    }
    if (month === 24) {
        this.price += 0.15
    }
    if (month === 12) {
        this.price += 0.3
    }
}
if (defaultMonth === 24) {
    if (month === 60) {
        this.price -= 0.35
    }
    if (month === 48) {
        this.price -= 0.25
    }
    if (month === 36) {
        this.price -= 0.15
    }
    if (month === 24) {
        this.price += 0.0
    }
    if (month === 12) {
        this.price += 0.15
    }
}
if (defaultMonth === 12) {
    if (month === 60) {
        this.price -= 0.5
    }
    if (month === 48) {
        this.price -= 0.4
    }
    if (month === 36) {
        this.price -= 0.3
    }
    if (month === 24) {
        this.price -= 0.15
    }
    if (month === 12) {
        this.price += 0.0
    }
}
If I were doing this, I'd start by putting the data into a 2 dimensional array. As raw data, it seems to be pretty much this:




default
12
24
36
48
60




36
-0.2
-0.1
0.0
0.15
0.3


48
-0.1
0.0
0.1
0.25
0.4


60
0.0
0.1
0.2
0.35
0.5




I don't do a lot of JavaScript, but I think in JavaScript syntax, that works out to something on this general order:
var priceTable = { 
    &quot;36&quot;: { &quot;12&quot;: -0.2, &quot;24&quot;: -0.1, &quot;36&quot;: 0.0, &quot;48&quot;: 0.15, &quot;60&quot;: 0.3 }, 
    &quot;48&quot;: { &quot;12&quot;: -0.1, &quot;24&quot;:  0.0, &quot;36&quot;: 0.1, &quot;48&quot;: 0.25, &quot;60&quot;: 0.4 }, 
    &quot;60&quot;: { &quot;12&quot;:  0.0, &quot;24&quot;:  0.1, &quot;36&quot;: 0.2, &quot;48&quot;: 0.35, &quot;60&quot;: 0.5 } 
};

this.price += priceTable[defaultMonth][month];