Created
August 31, 2023 18:26
-
-
Save mizanmahi/e4f8a654c141ecb5bfd97ccdbf1d6e5a to your computer and use it in GitHub Desktop.
unit calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const unitRateInfo = { | |
andhrapradesh: [ | |
{ | |
minUnit: 1, | |
maxUnit: 30, | |
totalUnit: 30, | |
rate: 1.9, | |
maxCost: 57, | |
}, | |
{ | |
minUnit: 31, | |
maxUnit: 75, | |
totalUnit: 45, | |
rate: 3, | |
maxCost: 135, | |
}, | |
{ | |
minUnit: 76, | |
maxUnit: 125, | |
totalUnit: 50, | |
rate: 4.5, | |
maxCost: 225, | |
}, | |
{ | |
minUnit: 126, | |
maxUnit: 225, | |
totalUnit: 100, | |
rate: 6, | |
maxCost: 600, | |
}, | |
{ | |
minUnit: 226, | |
maxUnit: 400, | |
totalUnit: 175, | |
rate: 8.75, | |
maxCost: 1531.25, | |
}, | |
{ | |
minUnit: 401, | |
rate: 9.75, | |
}, | |
], | |
andamannicobar: [ | |
{ | |
minUnit: 1, | |
maxUnit: 100, | |
totalUnit: 100, | |
rate: 2.25, | |
maxCost: 225, | |
}, | |
{ | |
minUnit: 101, | |
maxUnit: 200, | |
totalUnit: 100, | |
rate: 5, | |
maxCost: 1000, | |
}, | |
{ | |
minUnit: 201, | |
maxUnit: 500, | |
totalUnit: 300, | |
rate: 7.2, | |
maxCost: 3600, | |
}, | |
{ | |
minUnit: 501, | |
rate: 7.5, | |
}, | |
], | |
assam: [ | |
{ | |
minUnit: 1, | |
maxUnit: 120, | |
totalUnit: 120, | |
rate: 5.3, | |
maxCost: 636, // 120 * 5.3 | |
}, | |
{ | |
minUnit: 121, | |
maxUnit: 240, | |
totalUnit: 120, | |
rate: 6.6, | |
maxCost: 1584, // 240 * 6.6 | |
}, | |
{ | |
minUnit: 241, | |
rate: 7.6, | |
}, | |
], | |
bihar: [ | |
{ | |
minUnit: 1, | |
maxUnit: 100, | |
totalUnit: 100, | |
rate: 6.1, | |
maxCost: 610, // 100 * 6.1 | |
}, | |
{ | |
minUnit: 101, | |
maxUnit: 200, | |
totalUnit: 100, | |
rate: 6.95, | |
maxCost: 1390, // 200 * 6.95 | |
}, | |
{ | |
minUnit: 201, | |
rate: 8.05, | |
}, | |
], | |
chandigarh: [ | |
{ | |
minUnit: 1, | |
maxUnit: 150, | |
totalUnit: 150, | |
rate: 2.5, | |
maxCost: 375, // 150 * 2.5 | |
}, | |
{ | |
minUnit: 151, | |
maxUnit: 400, | |
totalUnit: 250, | |
rate: 4.25, | |
maxCost: 1700, // 400 * 4.25 | |
}, | |
{ | |
minUnit: 401, | |
rate: 4.65, | |
}, | |
], | |
chattisgarh: [ | |
{ | |
minUnit: 1, | |
maxUnit: 100, | |
totalUnit: 100, | |
rate: 3.7, | |
maxCost: 370, // 100 * 3.7 | |
}, | |
{ | |
minUnit: 101, | |
maxUnit: 200, | |
totalUnit: 100, | |
rate: 3.9, | |
maxCost: 780, // 200 * 3.9 | |
}, | |
{ | |
minUnit: 201, | |
maxUnit: 400, | |
totalUnit: 200, | |
rate: 5.3, | |
maxCost: 2120, // 400 * 5.3 | |
}, | |
{ | |
minUnit: 401, | |
maxUnit: 600, | |
totalUnit: 200, | |
rate: 6.3, | |
maxCost: 3780, // 600 * 6.3 | |
}, | |
{ | |
minUnit: 601, | |
rate: 7.9, | |
}, | |
], | |
}; | |
const calculateTotalUnits = (stateName, totalCost, unitRateInfo) => { | |
const stateInfo = unitRateInfo[stateName?.toLowerCase()?.replace(' ', '')]; | |
if (!stateInfo) { | |
console.log('State not found in unitRateInfo'); | |
return 0; | |
} | |
let totalUnits = 0; | |
for (const tier of stateInfo) { | |
if (tier.maxCost) { | |
if (totalCost >= tier.maxCost) { | |
totalUnits += tier.totalUnit; | |
totalCost -= tier.maxCost; | |
} else { | |
totalUnits += Math.floor(totalCost / tier.rate); | |
return totalUnits; | |
} | |
} else { | |
totalUnits += Math.floor(totalCost / tier.rate); | |
break; | |
} | |
} | |
return totalUnits; | |
}; | |
// Example usage | |
const stateName = 'andhrapradesh'; | |
const totalCost = 200; | |
const totalUnits = calculateTotalUnits(stateName, totalCost, unitRateInfo); | |
console.log(`Total units used in ${stateName}:`, totalUnits); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment