I am new to coding and currently touch the javascript part. I am facing a question about how to calculate the different tier prices and charges as shown in the image below.enter image description here.This is the code that I try myself which i is means the LOAN AMOUNT when i is equal to 1 it will use the next tier percentage to count and the i will +1 ag, but I am not sure is it correct or not... And should I add a EventListener to determine which tier the input now is?Sorry for my broken english.Thanks a lot!^_^
if(loan >= 500000 && loan < 1000000 && i==0) charge = 1% i = 1
if(loan >= 500000 && loan < 1000000 && i==1) charge = 0.8% i = 2
if(loan >= 2000000 && loan < 2000000 && i==2) charge = 0.7% i = 3
if(loan >= 2000000 && loan < 2000000 && i==3) charge = 0.6% i= 4
Not quite sure what your goal is, but if you want a system like tax brackets, where the loan amount is charged in portions based on the remaining money and where it fits in a tier it might look a little like this:
const getPercentageCharge = (amount) => {
let loanAmount = amount;
let i = [];
let percents = [];
loanAmount -= 500000;
i.push(1)
percents.push(1)
if (loanAmount < 0) {
return {i, percents}
}
loanAmount -= 500000;
i.push(2)
percents.push(0.8)
if (loanAmount < 0) {
return {i, percents}
}
loanAmount -= 2000000;
i.push(3)
percents.push(0.7)
if (loanAmount < 0) {
return {i, percents}
}
loanAmount -= 2000000;
i.push(4)
percents.push(0.6)
if (loanAmount < 0) {
return {i, percents}
}
loanAmount -= 25000000;
i.push(5)
percents.push(0.5)
if (loanAmount < 0) {
return {i, percents}
}
loanAmount -= 75000000;
i.push(0.5)
percents.push(6)
if (loanAmount < 0) {
return {i, percents}
}
}
console.log(getPercentageCharge(800000))
Otherwise, if you want the loan amount to be charged based on its total amount, it might look a little like this:
const getPercentageCharge = (amount) => {
let loanAmount = amount;
let i=1;
let percent=1.0;
if(loanAmount <= 500000) {
percent = 1.0;
i = 1;
}
if(loanAmount <= 1000000 && i==1) {
percent = 0.8;
i = 2
}
if(loanAmount <= 3000000 && i==2) {
percent = 0.7;
i = 3
}
if(loanAmount <= 5000000 && i==3) {
percent = 0.6;
i = 4;
}
if(loanAmount <= 7500000 && i==4) {
percent = 0.5;
i = 5;
}
if(loanAmount > 15000000 && i==5) {
percent = 0.5;
i = 6;
}
return {i, percent};
}
console.log(getPercentageCharge(800000))