I have a ton of if
statements that I am tasked to refactor and make more readable.
The if
statements rely on 2/3 JSON keys and I cannot think of a good way to make it better.
Does anyone have any tips for me?
These if
statements were nested inside some other if
statements, but I used a rules engine to help break it down. But I feel like a rules engine might not break this down to a better solution.
I’m thinking something like a map or lookup table, but I can’t process how to do this.
if (LM <= 15) {
if (COMWT <= 25000) {
COST = 225.0;
}
if (COMWT > 25000) {
if (COMWT <= 30000) {
COST = 275.0;
}
}
}
if (LM > 15) {
if (LM <= 25) {
if (COMWT <= 25000) {
COST = 250;
}
if (COMWT > 25000) {
if (COMWT <= 30000) {
COST = 300.0;
}
}
}
}
if (LM > 25) {
if (LM <= 40) {
if (COMWT <= 25000) {
COST = 275;
}
if (COMWT > 25000) {
if (COMWT <= 30000) {
COST = 325.0;
}
}
}
}
if (LM > 40) {
if (LM <= 55) {
if (COMWT <= 25000) {
COST = 325;
}
if (COMWT > 25000) {
if (COMWT <= 30000) {
COST = 350.0;
}
}
}
}
if (LM > 55) {
if (LM <= 70) {
if (COMWT <= 25000) {
COST = 375;
}
if (COMWT > 25000) {
if (COMWT <= 30000) {
COST = 400.0;
}
}
}
}
if (LM > 70) {
if (LM <= 85) {
if (COMWT <= 25000) {
COST = 425;
}
if (COMWT > 25000) {
if (COMWT <= 30000) {
COST = 450.0;
}
}
}
}
if (LM > 85) {
if (LM <= 100) {
if (COMWT <= 25000) {
COST = 475;
}
if (COMWT > 25000) {
if (COMWT <= 30000) {
COST = 500.0;
}
}
}
}
if (LM > 100) {
if (LM <= 125) {
if (COMWT <= 25000) {
COST = 525;
}
if (COMWT > 25000) {
if (COMWT <= 30000) {
COST = 550.0;
}
}
}
}
if (LM > 125) {
if (LM <= 150) {
if (COMWT <= 25000) {
COST = 575;
}
if (COMWT > 25000) {
if (COMWT <= 30000) {
COST = 600.0;
}
}
}
}
if (LM > 150) {
if (LM <= 300) {
if (COMWT <= 25000) {
COST = LM * 3;
}
if (COMWT > 25000) {
if (COMWT <= 30000) {
COST = LM * 3.5;
}
}
}
}
if (LM > 300) {
if (COMWT <= 25000) {
COST = LM * 2.5;
}
if (COMWT > 25000) {
if (COMWT <= 30000) {
COST = LM * 3;
}
}
}
CodePudding user response:
Store the checks in a variable and start with the greatest and work your way down to the lowest
const comwtLow = COMWT <= 25000;
const comwtHigh = COMWT > 25000 && COMWT <= 30000
let COST;
if (LM > 300 && comwtLow) COST = LM * 2.5;
else if (LM > 300 && comwtHigh) COST = LM * 3;
else if (LM > 150 && comwtLow) COST = LM * 3;
else if (LM > 150 && comwtHigh) COST = LM * 3.5;
else if (LM > 125 && comwtLow) COST = 575;
else if (LM > 125 && comwtHigh) COST = 600;
....
Without if/else you can do a data structure with an object and store the base part of the fees. Use a loop to find the fee structure that matches and generate the cost.
const fees = {
300: {
low: { multi: 2.5 },
high: { multi: 3 },
},
150: {
low: { multi: 3 },
high: { multi: 3.5 },
},
125: {
low: { flat: 575 },
high: { flat: 600 },
},
0: {
low: { flat: 225 },
high: { flat: 275 },
},
}
const feesValues = Object.keys(fees).map(Number).sort((a,b)=>b-a);
function getCost (lm, comwt) {
const range = comwt <= 25000 ? "low" : "high";
const feeData = fees[feesValues.find(value => value < lm)];
const { multi = 0, flat = 0 } = feeData[range];
return lm * multi flat;
}
console.log(getCost(400, 10000));
console.log(getCost(130, 10000));
This code expects there to be matches on everything. If that is not the case, you need to add more logic and protection around it.
CodePudding user response:
You can do it by storing it into an array of objects.
Note: Please verify all conditions before using this code
const conditionList = [
{
high: 15,
low: 0,
comwt: [
{
high: 25000,
low: 0,
cost: lm => 225
},
{
high: 30000,
low: 25000,
cost: lm => 275
}
]
},
{
high: 25,
low: 15,
comwt: [
{
high: 25000,
low: 0,
cost: lm => 250
},
{
high: 30000,
low: 25000,
cost: lm => 300
}
]
},
{
high: 40,
low: 25,
comwt: [
{
high: 25000,
low: 0,
cost: lm => 275
},
{
high: 30000,
low: 25000,
cost: lm => 325
}
]
},
{
high: 55,
low: 40,
comwt: [
{
high: 25000,
low: 0,
cost: lm => 325
},
{
high: 30000,
low: 25000,
cost: lm => 350
}
]
},
{
high: 70,
low: 55,
comwt: [
{
high: 25000,
low: 0,
cost: lm => 375
},
{
high: 30000,
low: 25000,
cost: lm => 400
}
]
},
{
high: 85,
low: 70,
comwt: [
{
high: 25000,
low: 0,
cost: lm => 425
},
{
high: 30000,
low: 25000,
cost: lm => 450
}
]
},
{
high: 100,
low: 85,
comwt: [
{
high: 25000,
low: 0,
cost: lm => 475
},
{
high: 30000,
low: 25000,
cost: lm => 500
}
]
},
{
high: 125,
low: 100,
comwt: [
{
high: 25000,
low: 0,
cost: lm => 525
},
{
high: 30000,
low: 25000,
cost: lm => 550
}
]
},
{
high: 150,
low: 125,
comwt: [
{
high: 25000,
low: 0,
cost: lm => 575
},
{
high: 30000,
low: 25000,
cost: lm => 600
}
]
},
{
high: 300,
low: 150,
comwt: [
{
high: 25000,
low: 0,
cost: lm => lm * 3
},
{
high: 30000,
low: 25000,
cost: lm => lm * 3.5
}
]
}
];
function cal(lm, cmwt) {
let costd = 0;
conditionList.forEach(item => {
if (lm >= item.low && lm <= item.high) {
item.comwt.forEach(c => {
if (cmwt >= c.low && cmwt <= c.high) {
costd = c.cost(lm);
}
});
}
});
return costd;
}
console.log(cal(75, 24000));